5

我有一个需要通过 Amazon API Gateway 触发的 lambda 函数。有没有办法将已经存在的 API(使用 AWS 控制台创建)包含到 AWS SAM 模板中?

4

1 回答 1

2

SAM 尚不支持模板中的 !ImportValue。

Github 上的问题

在 aws/serverless-application-model 的 GitHub 上,有一个针对该功能的开放 PR

看这里

如果您愿意,您可以帮助并为该 PR 做出贡献,那么您可以开始在您的 SAM template.yml 中使用 !ImportValue

否则,我建议您使用旧方法,使用 CloudFormation 模板创建 CI/CD,该模板可以使用 !ImportValue 并链接到您的 lambda 函数代码所在的 S3 存储桶。

Cloudformation 模板示例

更新

SAM CLI 现在支持 !ImportValue,Github 上的问题已关闭。

您可以按如下方式使用它

# You need to export the resource that you want to use in another template first

# This goes at the end of your template.yml file, after the Resources

# template.yml in the first repo
Outputs:
    myExportedResource:
        Value: !Ref TheResource
        Export:
            Name: !Sub "{environment}-nice-export-name"


# template.yml in the second repo (This obviously goes in Resources)
    MyLambda:
        Type: AWS::Serverless::Function
        Properties:
            FunctionName: awesome-lambda
            CodeUri: ./dist
            Handler: this-file.handler
            Role: !GetAtt LambdaRole.Arn
            VpcConfig:
                SecurityGroupIds:
                    - !GetAtt SecurityGroup.GroupId
                SubnetIds:
                    - Fn::ImportValue: !Sub "${environment}-nice-export-name"

您可以将其完全用作普通的 cloudformation 模板

https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-importvalue.html

请注意,这里我使用 Fn:ImportValue 因为我需要使用 !Sub 但如果您不需要在导入值中引用参数,只需使用 !ImportValue

于 2020-12-13T18:05:48.563 回答