Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 275
red888 Avatar asked Sep 11 '18 13:09

red888


People also ask

What is not captured in the CloudFormation template?

CloudFormation does not transform, modify, or redact any information you include in the Metadata section. For more information, see Metadata. The Outputs template section.

Which file formats can be used with CloudFormation templates?

You can author AWS CloudFormation templates in JSON or YAML formats. We support all AWS CloudFormation features and functions for both formats, including in AWS CloudFormation Designer.

How do you reference existing resources in CloudFormation?

To import existing resources into a CloudFormation stack, you need to provide: A template that describes the entire stack, including both the resources to import and (for existing stacks) the resources that are already part of the stack. Each resource to import must have a DeletionPolicy attribute in the template.

Which section of CloudFormation template does not allow for conditions?

According to the docs, Conditions should be used at the top level of the resource you want to conditionally create. Putting a Condition inside the Instance UserData section isn't supported. To use Conditions in your situation, you'd want separate Resources conditionally created based on the Parameter.

Can we create CloudFormation template from existing resources?

Create a stack from existing resources using the AWS Management Console. Sign in to the AWS Management Console and open the AWS CloudFormation console at https://console.aws.amazon.com/cloudformation . On the Stacks page, choose Create stack, and then choose With existing resources (import resources).

What part of a CloudFormation template allows you to pass values into the template?

Parameters (optional) Values to pass to your template at runtime (when you create or update a stack). You can refer to parameters from the Resources and Outputs sections of the template.


1 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.

like image 166
yorodm Avatar answered Sep 23 '22 22:09

yorodm