zdenko.s
zdenko.s

Reputation: 1032

How to use CloudFormation template from S3

There is a section in documentation Use an Existing AWS CloudFormation Template. It uses sync function from file system (fs.readFileSync). I am using CloudFormation template from S3 EKS Public and private subnets. According to my understanding AWS CDK is sync and I cannot use request library to fetch existing template. There is an option to download yaml file to local file system.

I would like to use template directly from S3, if possible.

Upvotes: 2

Views: 377

Answers (1)

Max Schenkelberg
Max Schenkelberg

Reputation: 855

Sure, just use the aws-sdk to get the s3 contents from the bucket it's in.

async getCfnIncludeFromS3(): Promise<cdk.CfnInclude> {
  const s3 = new aws.S3();
  const template = await s3.getObject({Bucket: "MyBucket", Key: "My/Key/To/Template.json"}).promise();

  return new cdk.CfnInclude(this, "ExistingInfrastructure", {
    template: JSON.parse(template.Body).toString()
  });
}

Upvotes: 1

Related Questions