I created a Webserver stack with an EC2 instance and I am trying to deploy a local artifact to the EC2 instance using CloudFormation::Init
I tried simply referencing the local file website.zip
under sources:
and running cloudformation package
, but unfortunately it only recognizes and uploads specific resources to S3.
Metadata:
AWS::CloudFormation::Init:
config:
packages:
yum:
npm: []
sources:
/home/ec2-user/developer-site: applications/website.zip
I also tried using AWS::Include
in combination with cloudformation package
, but AWS ::Include
substitutes the raw contents of the file rather than simply the s3 url.
Is there another way around this? Is it possible to create my own macro for replacing [local url] with [s3 url] under Metadata sources? It seems like such a useful feature, I can’t imagine it not being possible.
2
Answers
I think the only way to do it is a two-step. Upload the ZIPs to the S3 bucket with the CloudFormation template. Ensure the EC2 instance has the correct role to access the zip file in the S3 bucket. In the EC2 CloudFormation section, use the userdata section to run script to issue aws s3 cp commands to get the zip file into S3. Bonus! You can also unzip it and do all kinds of other things.
Agreed with Christian. The only other suggestion would be to use CDK which allow you to bundle assets and upload in one operation. It may be overkill for what you are thinking. SAM and AWS CLI both have options to bundle assets but neither of them support doing that for AWS::CloudFormation::Init at this time. https://github.com/aws/aws-cli/issues/2862