Updating a CloudFormation stack if codebase updates - amazon-ec2

So I have an existing CloudFormation stack up and running. However, I haven't found a solution for my problem, which is that I want my resources, for example EC2 and Lambda, to have up to date code.
It seems that a CloudFormation stack doesn't update if the template doesn't have any changes. I'm holding my code inside a S3 bucket as a zip-file, but if this file gets changed, CloudFormation doesn't notice it.
Is my best bet creating a git hook script that uses AWS CLI and updates the EC2 and Lambda code or is there some 'elegant' way for CloudFormation to notice these changes?

Create a new lambda function to update your existing lambda and ec2 or call the cloud formation to update them. On your S3, create an object Put event and call that new lambda function. So whenever a new file(zip) is put in s3, your ec2 & lambda gets updated.

Related

Is there a way to deploy a terraform file via an AWS lambda function?

As the title suggests I am looking for a way to deploy a terraform file via an AWS lambda function. I would like to deploy this file via a time-based event. This is my first time working with terraform and I cannot seem to find anything pertaining to this specific use case.
I am much more versed in CloudFormation so normally what I would do is use the boto3 library to set up a lambda function that would deploy a CloudFormation stack. Does anyone know how to do this with a terraform file?

How to apply Serverless to an existing Lambda

When I try deploy to an existing lambda function configured in serverless.yml as following, it says "An error occurred: ApiLambdaFunction - an-existing-function-name-created-by-my-devops already exists."
functions:
api:
name: an-existing-function-name-created-by-my-devops
So it is not allowed to deploy to an existing lambda not created by serverless?
As Serverless manages your resources via a CloudFormation Stack, you could probably be able to import the lambda function within the UI (Import Existing Resources into a CloudFormation Stack) and do the deploy afterwards again.
I did not try this and there's most probably a better solution though.
Edit: precondition is that you successfully created your stack before adding your desired function.

Forced redeployment of Lambda function from S3?

I have a Lambda function defined in a Cloudformation template with a reference to an S3 bucket and key where I have saved a zipfile containing the Lambda source in the usual fashion. I have a separate CI build process building the Lambda function and dumping it into S3. Now I want the S3 key within the Cloudformation template to be static, I don't want to be changing it for every Lambda commit+rebuild. But Cloudformation thinks the Lambda hasn't changed because the S3 key hasn't changed, even though the contents of the zipfile have been changed.
Must I change the S3 key each time to trigger Lambda redeployment, or is there a way to force Lambda redeployment via Cloudformation whilst retaining the static key ?
You are right, CFT doesn't realise the changes since the S3 key remains same despite the content of it is changed.
As you mentioned, can have the S3 Key different from the previous CFT execution so that the lambda code gets deployed.
You will have to keep the S3 key as CFT parameter
Otherwise, try using SAM Packaging in AWS Code Build and use Code Deploy with Cloud Formation.
Here, the location will not be mentioned as zip, instead takes the code path and builds it and template gets updated with the new deployment package location everytime. (See buildspec.yml in CodeBuild)
References:
https://docs.aws.amazon.com/lambda/latest/dg/build-pipeline.html
https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-deploying.html
Hope this helps.

CFN Take latest AMI and launch a EC2 Instance

One of my developer is creating a Images and they are storing in AWS, and I can see them under Images --> AMI.
Now here my request starts -
I want to take the latest AMI and launch EC2. So how can I write JSON to call the latest AMI and launch EC2.
Can you please help me quickly. Thanks in Advance!
You have two options:
Determine the AMI to use, and then pass that value to the CloudFormation template as a Parameter. Whatever code you use to launch the template would be responsible for doing a lookup on the AMI to use, so it is happening before CloudFormation is called.
Use a Custom Resource in CloudFormation that can call a Lambda function, which would determine the AMI to use. The Lambda function would return the value to CloudFormation, which would then use that value to launch the instance.
Either way, you would need to write code to perform your logic. It cannot be done within the JSON itself.

AWS CloudFormation: AMI in LaunchConfiguration was deleted

As the title says, I have a CFN stack that uses a LaunchConfiguration for EC2. The config gets the AMI-id from a Lambda function that looks up the latest AMI for a particular type. This is very similar to what's described here
The AMI that was used in creating the launch-config has been deleted. When I try to update the stack I get an expected error:
UPDATE_FAILED AWS::AutoScaling::LaunchConfiguration ECSLaunchConfiguration AMI cannot be described
I cannot delete the stack, so is there any way to resolve this using CFN. A new AMI is out there, so how I get CFN to re-create the config by re-running the Lambda function?

Resources