AWS SAM:我们能在AWS SAM模板中使用已经存在的API吗?

5
我有一个需要通过Amazon API Gateway触发的Lambda函数。是否有一种方式可以将已经存在的API(使用AWS控制台创建)包含到AWS SAM模板中?

你可以使用AWS::Serverless::Api,你查看了这个页面吗?https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-api.html - ZEE
这将创建一个新的API,但我想在模板文件中使用已经存在的API。 - A Teja
1个回答

3
SAM暂时不支持模板中的!ImportValue。 在Github上的问题 在aws/serverless-application-model的GitHub上,有一个开放的PR来实现这个功能。 在这里查看 如果您愿意,可以帮助并为该PR做出贡献,那么您就可以开始在SAM模板.yml中使用!ImportValue了。
否则,我建议您采用旧的方式,创建一个CI/CD与CloudFormation模板配合使用,可以使用!ImportValue,并链接到一个S3存储桶,其中包含您的Lambda函数代码。 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"


你可以像使用普通的云化模板一样使用它。

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

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

有没有关于如何使用!ImportValue的文档? - Mooncrater
1
@Mooncrater 我更新了我的回复,并提供了一个代码示例。您可以像使用普通的cloudformation !ImportValue 一样使用它。请确保拥有最新的sam cli版本。您可以在shell中输入sam --version来检查版本。请确保至少使用版本1.24.1。 - Benjamin Filiatrault
@Mooncrater AWS 文档在这里: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-importvalue.html - abk

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接