ColossusMark1
ColossusMark1

Reputation: 1289

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 .

Upvotes: 4

Views: 6534

Answers (3)

hammad
hammad

Reputation: 61

According to this doc - The value of an output can include literals, parameter references, pseudo-parameters, a mapping value, or intrinsic functions.

Given that, AWS::CloudFormation::Init is a CloudFromation Metadata specific type but not a literal, parameter, pseudo-parameter, mapping value, or intrinsic function.

So, what you are trying to achieve is appearing to be as a CloudFormation Outputs limitation :(

@m-jensen answer lists many suitable workarounds for this.

Upvotes: 0

M Jensen
M Jensen

Reputation: 566

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.

Upvotes: 10

Rafał Wrzeszcz
Rafał Wrzeszcz

Reputation: 2057

In comments to your question there are some valid points to push you towards correct solution. In general what you try to do is impossible - there are no outputs of AWS::CloudFormation::Init resource. They are not evaluated at stack creation time, just stored and processed by EC2 instance on it's launch time, but it's not the time CloudFormation stack is running (may be, but not always - take auto scaling group for example).

Upvotes: 3

Related Questions