将ARN参考从CloudFormation传递到Swagger [英] Passing ARN reference from CloudFormation to Swagger

查看:82
本文介绍了将ARN参考从CloudFormation传递到Swagger的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在尝试使用Amazon CloudFormation和Swagger自动化AWS lambda和API网关的部署。为此,我们创建了一个CloudFormation模板来创建Lambda和APIGateway所需的其他资源(包括端点)。我们想从外部swagger文件中导入API定义,以便同一CloudFormation模板可用于多个lambda和APIGateways。有没有一种方法可以引用由CloudFormation模板创建的lambda的ARN在包含API定义的外部swagger文件(在同一CloudFormation模板中引用)中?

We are trying to automate the deployment of AWS lambda and API gateway using Amazon CloudFormation and Swagger. Towards this, we have created a CloudFormation template to create the Lambda and other resources required for APIGateway (including the endpoints). We would like to import the API definitions from an external swagger file so that the same CloudFormation template can be used for multiple lambdas and APIGateways. Is there a way we can refer the ARN of the lambda which has been created by the CloudFormation template in the external swagger file (being referred to in the same CloudFormation template) which holds the API definition?

摇摇欲坠的内容:

"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"
            }

在上述集成方法中,我需要从云形成模板中动态替换uri的值。

In the above integration method I need to replace the value of the uri dynamically from the cloud formation template.

我的云形成脚本如下:

"myApi":{
      "Type" : "AWS::ApiGateway::RestApi",
      "Properties" : {
        "BodyS3Location" : S3Location of the swagger definition file,
        ..,
        ..
      }
    }


推荐答案

新解决方案:

现在可以使用新的 AWS :: Include 转换以直接从CloudFormation模板引用上传的模板:

It is now possible to use the new AWS::Include Transform to reference the uploaded template directly from a CloudFormation template:

Api:
  Type: AWS::ApiGateway::RestApi
  Properties:
    Body:
      Fn::Transform:
        Name: AWS::Include
        Parameters:
          Location: !Sub s3://${ArtifactsBucket}/swagger.yaml

其中 ArtifactsBucket 指的是您在创建或更新堆栈之前上传Swagger规范的存储桶。然后,在Swagger模板本身中,您可以使用内部函数,例如

where ArtifactsBucket refers to the bucket where you upload Swagger spec before you create or update your stack. Then, in the Swagger template itself you can use the intrinsics, e.g.

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

这里我使用的是长 Fn :: Sub 表示法,而不仅仅是!Sub 表示法,因为Swagger本身不支持后者,也因为 AWS :: Include 转换表示尚不支持速记形式。

Here I'm using the long Fn::Sub notation instead of just !Sub, because Swagger doesn't natively support the latter, and also because the docs on AWS::Include Transform say that shorthand forms are not supported yet.

您也可以使用 AWS :: Serverless :: Api DefinitionBody (如果使用 SAM

You can also use AWS::Serverless::Api and DefinitionBody if you use SAM.

旧的解决方法:

另一种(有点棘手,但很简单)解决方案是将Api列为CloudFormation模板中的最后一个资源,并指定一个正文,末尾带有 :! Sub |-

Another (somewhat hacky, yet simple) solution is to list the Api as the last resource in the CloudFormation template, and specify an empty Body with : !Sub |- at the end.

您可以然后将此模板与实际的Swagger文件连接起来,并使用该文件中的标准 $ {} 语法引用任何参数。

You can then concatenate this template with the actual Swagger file, and reference any parameters using the standard ${} syntax in that file.

唯一的小麻烦是,使用YAML时,在连接Swagger文件时必须正确缩进它(这种方法不适用于JSON模板;如果使用这些,则必须用 jq 之类的东西代替Body。)

The only minor complication is that you have to properly indent the Swagger file when concatenating it when you're using YAML (this approach won't work for JSON templates though; you'll have to substitute Body with something like jq if you use these).

这篇关于将ARN参考从CloudFormation传递到Swagger的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆