0
votes

I need to create an S3 bucket and a lambda function using CloudFormation. I have the jar file in my local. If I write resources for the S3 bucket and a lambda function in a single template, I have to provide S3bucket and key in the lambda resource. Stack creation fails, as the jar file doesn't exist in the bucket. So, does this mean that I have to create a bucket separately using a template, upload the jar file, and then create a lambda function using another template?

Is there a way to create both the resources using one template?

2

2 Answers

1
votes

For some resource properties that require an Amazon S3 location (a bucket name and filename), you can specify local references. Instead of manually uploading the files to an S3 bucket and then adding the location to your template, you can specify local artifacts in your template and then use the aws cloudformation package command to quickly upload them.

You can find more info here: Uploading Local Artifacts to an S3 Bucket

1
votes

So, does this mean that I have to create a bucket separately using a template, upload the jar file, and then create a lambda function using another template?

Yes and no.

Normally when you create a bucket it will be empty. You can't populate it with vanilla CloudFormation. Normally you would have to manually (e.g. using AWS CLI, SDK or using console) upload the jar file.

For more advanced solution to keep everything inside CloudFormation you would have to look at creating your own Custom Resources in CloudFormation which would upload the jar file for you in CloudFormation. For this, your jar would need to be available online so that it can be downloaded into your bucket.

So if you are just starting with CloudFormation it will be probably to difficult to create a custom resource at first.