I'm setting up an auto-scaling group using cloud formation, and running a script via UserData on server startup. At the end of the user data script, I'm attempting to call cfn-signal to let the autoscaling group know that instance startup is complete:
cfn-signal -s true --stack stack_name --resource resource_name --region region_name
However, I'm receiving the following error:
cfn-signal: command not found
cfn-signal is definitely installed on the machine:
[ec2-user#ip-xxx-xx-xx-xx ~]$ which cfn-signal
/opt/aws/bin/cfn-signal
It turns out that the symlink to cfn-signal must not be created until after the UserData script is run.
I was able to use cfn-signal by calling it with the full path /opt/aws/bin/cfn-signal
Related
I have created a template where I created EC2 instance and I used cfn-init to process the configsets, and in the Userdata section of the instance, I wrote some commands to be executed by cloud-init and some commands to be executed without cloud-init.
I am not sure which commands runs in which sequence?
What I mean is, in which order the commands are executed? For example:
Commands in the configsets
Commands in the cloud-init section of the userdata
Commands in the Userdata
Part of my code is below:
UserData:
Fn::If:
- DatadogAgentEnabled
-
Fn::Base64: !Sub |
#!/bin/bash -xe
yum update -y
yum update -y aws-cfn-bootstrap
/opt/aws/bin/cfn-init --stack ${AWS::StackName} --resource GhostServer --configsets prepare_instance_with_datadog --region ${AWS::Region}
/opt/aws/bin/cfn-signal -e $? --stack ${AWS::StackName} --resource GhostServer --region ${AWS::Region}
#cloud-config <----cloud-init section inside the Userdata
runcmd:
- [ sh, -c, "sed 's/api_key:.*/api_key: {DatadogAPIKey}/' /etc/datadog-agent/datadog.yaml.example > /etc/datadog-agent/datadog.yaml" ]
- systemctl restart datadog-agent
From the AWS documentation
UserData :
The user data to make available to the instance. For more
information, see Running commands on your Linux instance at launch
(Linux) and Adding User Data (Windows). If you are using a command
line tool, base64-encoding is performed for you, and you can load the
text from a file. Otherwise, you must provide base64-encoded text.
User data is limited to 16 KB.
So Basically we define UserData to execute some commands at the launch of our EC2 instance.
Reference : https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-instance.html
To Answer your question first of all UserData commands will be executed and all the commands specified in this section will execute sequentially so in your example you invoked cfn helper scripts first and then cloud init thus configsets will be applied first and then cloud init commands will be called.
Inside that UserData section we are invoking cfn helper scripts which reads cloudformation template metadata and executes all the configsets defined under AWS::CloudFormation::Init:
From the AWS Documentation :
The cfn-init helper script reads template metadata from the
AWS::CloudFormation::Init key and acts accordingly to:
Fetch and parse metadata from AWS CloudFormation
Install packages
Write files to disk
Enable/disable and start/stop services
Reference : https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cfn-init.html
I want to perform a lambda invocation from terraform when destroy is running.
The terraform job is run by Jenkins on remote server.
According to this documentation I defined the following provisioner:
provisioner "local-exec" {
when = destroy
command = "aws lambda invoke --function-name ${var.lambda_name} --payload '{ \"someProperty\": \"someValue\" }' response.json"
}
the command syntax of lambda invoke is according to AWS CLI Command Reference
But, when running Terraform getting the following error:
Error running command 'aws lambda invoke --function-name my-lambda-name --payload '{ "someProperty": "someValue" }' response.json': exit status 127. Output: /bin/sh: aws: command not found
Why do I get /bin/sh: aws: command not found in local-exec provisioner?
local-exec is running on your local machine instead of the resource. So I would guess you are missing the aws-cli on your machine.
The command which aws will show whether or not you have aws-cli installed and available within your PATH? If it is not installed, follow the installation instructions for your OS.
successfully installed the EKS CLI on the terminal. But when I try to execute the command
aws eks --us-east-1 region update-kubeconfig --name codefresh
it showing an error saying
aws: error: argument command: Invalid choice
It would be great if someone helps me with the proper solution.
You have an error in your call. You specify region with --region us-east-1 and not with --us-east-1 region
I have 6 spot instances for a project. I only need them up for 15 mins then I need to terminate them to spin then up again.
My idea is as follow. Install aws cli within the AMI. Then create a cron job to terminate ec2 spot instance
Current : aws ec2 terminate-instances
However you need the instance ID, being a spot instance I do not know the ID.
--update--
Per suggestion below, did stop-instance test. Got error
aws ec2 stop-instances --instance-ids ec2metadata
An error occurred (InvalidInstanceID.Malformed) when calling the StopInstances operation: Invalid id: "ec2metadata" (expecting "i-...")
How can I terminate the instances within the instance I want to terminate?
curl -s http://169.254.169.254/latest/meta-data/instance-id
will get the instance-id of the instance. Something like:
aws ec2 stop-instances --instance-ids `curl -s http://169.254.169.254/latest/meta-data/instance-id`
or create a shell script and execute it as a cron job.
#!/bin/sh
instid = `curl -s http://169.254.169.254/latest/meta-data/instance-id`
aws ec2 stop-instances --instance-ids $instid
I understand the AWS::CloudFormation::Init block of the template is executed by cfn-init script.
Here is the syntax I use to do so:
/usr/local/bin/cfn-init -v --stack WebS<erver1 --resource WebServerInstance --region eu-central-1 --configset InstallAndRun && cat /var/log/cfn-init.log
I can do the call via UserData or via SSH.
Now I would like to have the possibility to modify the AWS::CloudFormation::Init part of the template and repeat the cnf-init call via ssh.
Question:
Where is the file with the cloud formation code located on my instance, which is executed by cfn-init?
The cfn-init command uses a configuration file located at /var/lib/cfn-init/data/metadata.json. The JSON file contains the config sets defined in the AWS::CloudFormation::Init block of a Cloud Formation template.
I've confirmed the path above on Amazon Linux 2. The location on other operating systems may be different.
All of the cloud init files are in /var/lib/cloud (on Centos 7) and the userdata script is in /var/lib/cloud/instance
See my other answer
cloud-init: What is the execution order of cloud-config directives?
for general hints on where the source to cloud-init is, execution ordering