Can I insert text from another file into my cloudformation template?

Viewed 5782

I have this for example in my template:

 ApiGatewayRestApi:
      Type: AWS::ApiGateway::RestApi
      Properties:
        Name: MyApi
        Description: My AWS API Gateway config
        Body:
          # INSERT swagger.yml content here

Is there some cloudformation function I can use to read swagger.yml in or attach it under "Body:"? So I can keep it in another file and my template doesn't become huge.

2 Answers

There's a Fn::Transform function that allows you to call different Cloudformation macros to process your templates. One of those macros is AWS::Include

Heres an example:

Resources:
  APIGateway:
    Fn::Transform:
      Name: AWS::Include
      Parameters:
        Location:
          Fn::Sub: s3://partials-bucket/${PartialsEnv}/resources/api-gateway.yaml

Here api-gateway.yaml will have the full definition of your resource.

You can use this function in the same way as other intrinsic functions. The only caveat is AWS::Include will only work with files hosted in S3 so you'll need to upload your partials separatedly.

You can try the BodyS3Location .

The Amazon Simple Storage Service (Amazon S3) location that points to an OpenAPI file, which defines a set of RESTful APIs in JSON or YAML format.

For Example

"BodyS3Location": {
          "Bucket": "you_bucket_name",
          "Key": "filename.yaml"
        }

For more see BodyS3Location

Related