Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cloudformation Output of Cloudformation Init

I have got a CloudFormation stack , shown as below ,

  "Metadata" : {
            "AWS::CloudFormation::Init" : {
                "config" : {

                        "/home/ec2-user/create_db_user.sh" : {
                            "source" :                             

   "http://s3.amazonaws.com/devops/create_db_user.sh",
                            "mode" : "000755",
                            "owner" : "ec2-user"
                        }
                    }
 ...

I need to run this command when EC2 instance is up , after i need set this init script output to the cloudformation stack .

How I can to this .

like image 372
ColossusMark1 Avatar asked Sep 01 '25 16:09

ColossusMark1


1 Answers

A bit late to the party but in addition to the previous answer...

The output of AWS::CloudFormation::Init at EC2 instance creation is not accessible through Cloudformation.

However on the EC2 instances that have been started you will find logs for both AWS::CloudFormation::Init (aka. cfn-init) and from user-data (cloud-init).

On Amazon Linux this is structured as follows:

  • /var/log/cfn-init-cmd.log: cfn-init and command output with timestamps
  • /var/log/cfn-init.log: cfn-init and command output
  • /var/log/cloud-init.log: cloud init logs pertaining to running the commands in user-data
  • /var/log/cloud-init-output.log: output from the user-data commands themselves

The simplest way to access those logs is to stream the logs to Cloudwatch Logs, which will enable you to search and filter the logs by time and content. Also consider using Cloudwatch Insights for search and filtering - it's got a more comprehensive and understandable syntax for search, filter and even simple visualisation (chart).

Seeing as you're already using cfn-init, you can find information here on how to install the new'ish Cloudwatch Agent (not Cloudwatch Logs Agent!). The benefit of using the Cloudwatch Agent is that it can also stream custom metrics to Cloudwatch Metrics (e.g. memory usage, app metrics, database metrics etc).

For already running EC2 instances, consider having a look at cfn-hup here.

The alternative would be to use SSM (Systems Manager) to push out the Agent onto already running instances as per instructions here.

like image 169
M Jensen Avatar answered Sep 04 '25 05:09

M Jensen