Home > Software design >  Query external resources from CloudFormation (Like terraform's data)
Query external resources from CloudFormation (Like terraform's data)

Time:10-26

I'm working with a CloudFormation template which is defining a lot of parameters for static values out of the scope of the template.

For example, the template is creating some EC2, and it has parameters for each VPC subnet. If this was Terraform, I would just remove all of these parameters and use data to fetch the information.

Is it possible to do that with CloudFormation?

Notice that I'm not talking about referencing another resource created within the same template, but about a resource that already exists in the account that could have been created by different means (manual, Terraform, CloudFormation, whatever...)

CodePudding user response:

No, CloudFormation does not have any native ability to look up existing resources. You can, however, achieve this using a Cloudformation macro.

A CloudFormation macro leverages a lambda function, which you can implement with whatever logic you need (e.g. using boto3) so that it returns the value you're after. You can even pass parameters to it.

Once the macro has been created, you can then consume it in your existing template.

You can find a full example on how to implement a macro, and on how to consume it, here: https://stackoverflow.com/a/70475459/3390419

CodePudding user response:

No, with "flat" cloudformation template you can't do it. Recently AWS released a new way to manage IAC wiht AWS CDK (https://docs.aws.amazon.com/cdk/v2/guide/home.html). in CDK there is some method (ex InstanceTarget) to fetch existing AWS resourse: take a look at How can I fetch existing EC2 Instances via Instance-name and add them as targets to ALB using AWS CDK in Python

AWS CDK How to reference subnets just created using CDK

I'm not sure if it help

  • Related