将 ARN 引用从 CloudFormation 传递给 Swagger [英] Passing ARN reference from CloudFormation to Swagger

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

问题描述

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

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 Transform 直接从 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 Transform 上表示尚不支持速记形式.

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.

如果您使用 SAM.

旧的解决方法:

另一个(有点老套,但很简单)的解决方案是将 Api 列为 CloudFormation 模板中的最后一个资源,并使用 指定一个 empty 正文:!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 模板;您必须将 Body 替换为 jq 如果你使用这些).

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天全站免登陆