Jef*_*y M 4 amazon-web-services aws-cloudformation swagger aws-lambda aws-api-gateway
我们正在尝试使用Amazon CloudFormation和Swagger自动部署AWS lambda和API网关.为此,我们创建了一个CloudFormation模板来创建APIGateway所需的Lambda和其他资源(包括端点).我们希望从外部swagger文件导入API定义,以便相同的CloudFormation模板可用于多个lambdas和APIGateways.有没有办法可以引用由外部swagger文件(在同一个CloudFormation模板中引用)中由CloudFormation模板创建的lambda的ARN,该文件包含API定义?
Swagger内容:
"x-amazon-apigateway-integration": {
"uri": "arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/arn:aws:lambda:us-east-1:123456789012:function:TestSimpleProxy/invocations",
"passthroughBehavior": "when_no_match",
"httpMethod": "POST",
"type": "aws_proxy"
}
Run Code Online (Sandbox Code Playgroud)
在上面的集成方法中,我需要从云形成模板中动态替换uri的值.
我的云形成脚本如下:
"myApi":{
"Type" : "AWS::ApiGateway::RestApi",
"Properties" : {
"BodyS3Location" : S3Location of the swagger definition file,
..,
..
}
}
Run Code Online (Sandbox Code Playgroud)
新解决方案:
现在可以使用新的AWS::IncludeTransform直接从CloudFormation模板引用上传的模板:
Api:
Type: AWS::ApiGateway::RestApi
Properties:
Body:
Fn::Transform:
Name: AWS::Include
Parameters:
Location: !Sub s3://${ArtifactsBucket}/swagger.yaml
Run Code Online (Sandbox Code Playgroud)
where ArtifactsBucket指的是在创建或更新堆栈之前上载Swagger规范的存储桶.然后,在Swagger模板中,您可以使用内在函数,例如
x-amazon-apigateway-integration:
type: aws_proxy
httpMethod: POST
uri:
Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${LambdaFunction.Arn}/invocations
Run Code Online (Sandbox Code Playgroud)
在这里,我使用的是长Fn::Sub符号而不仅仅是!Sub因为Swagger本身不支持后者,也因为关于AWS::IncludeTransform 的文档说不支持缩写形式.
您也可以使用AWS::Serverless::Api,DefinitionBody如果您使用SAM.
旧的解决方法:
另一个(有点hacky,但简单)解决方案是将Api列为CloudFormation模板中的最后一个资源,并在末尾指定一个空的 Body : !Sub |-.
然后,您可以将此模板与实际的Swagger文件连接,并使用该${}文件中的标准语法引用任何参数.
唯一的一个小问题是,当你使用YAML连接它时,你必须正确地缩进Swagger文件(这种方法不适用于JSON模板;你必须用像jq使用这些的东西替换Body ) .
这是"x-amazon-apigateway-integration"您的 CloudFormation 模板的一部分吗?
如果是这样,那么按照https://blog.jayway.com/2016/09/18/introduction-swagger-cloudformation-api-gateway/中的示例,我认为您可以使用Ref函数来传递该信息。
| 归档时间: |
|
| 查看次数: |
4675 次 |
| 最近记录: |