0

I have created a three step functions 1. create an ec2 instance 2. generate APK 3. shutdown machine

The first and third function works properly, but in the second function, I need to run a shell script which is in the ec2 instance to generate apk. So How can I run this script from step function?

Reza Mousavi
  • 4,420
  • 5
  • 31
  • 48
Nimesh
  • 1
  • 8
  • The fact that the script is being triggered from Lambda is no different to triggering it from anywhere on the Internet. You will need to provide a mechanism from your instance to allow the script to be externally triggered. Or, preferably, convert the script into something you can run under Lambda. – John Rotenstein Oct 09 '18 at 05:37
  • According to documentation I am sending a .txt file with shell script while creating a instance instance = EC2.run_instances( ImageId=AMI, InstanceType=INSTANCE_TYPE, KeyName="./automatebuild.pem", MinCount=1, # required by boto, even though it's kinda obvious. MaxCount=1, InstanceInitiatedShutdownBehavior='terminate', # make shutdown in script terminate ec2 UserData="./init.txt" ) script is not executing after creating an instance – Nimesh Oct 09 '18 at 06:05
  • In the EC2 console, select the instance, go to Instance Settings, then View/Change User Data. Confirm that the contents of your init.txt file is appearing. It should start with `#!`. If the User Data is not there, you probably need to pass it into the `run_instances` command differently (not by filename). The documentation actually states that it needs a **string**, not a filename, so your code will need to read the contents of the file and pass it into that parameter. – John Rotenstein Oct 09 '18 at 06:09
  • What is the script doing? – John Rotenstein Oct 09 '18 at 06:10
  • I have developed white label android app so when user click on "Submit" button ec2 instance should be created then run a script which will create ,build and generate an apk and then shutdown a machine – Nimesh Oct 09 '18 at 06:35
  • Any chance you can do that from Lambda instead of EC2? The only limitations are a run-time of 5 minutes and 500MB of disk space. – John Rotenstein Oct 09 '18 at 06:44
  • Actualy automate.sh script is in ec2 instance but when I create a instance i am only passing a .txt file which contain only ./automate.sh so after creating an instance in machine ./automate.sh will execute.....I have already created a custom AMI in which I have already installed all softwares and clone git repository. – Nimesh Oct 09 '18 at 07:05
  • I need one more help...how do i import .pem file in step function? – Nimesh Oct 09 '18 at 07:12
  • I presume you mean "in a Lambda function"? Import from where? If it is from S3, just use `copy_object()` to download it from a bucket. Or, it could be stored in the AWS Systems Manager Parameter Store. – John Rotenstein Oct 09 '18 at 21:54

1 Answers1

0

You have basically 2 options:

  1. create a lambda function in step 2 which you use to ssh into your previously generated ec2 instance. However if the instance is not fully started when the lambda function is invoked, you just get an error.

  2. create a new custom image on ebs which executes your script at startup. This would just postpone the problem from solution 1 until the shutdown command is executed but if you dump your idea of using step functions, you could just append the shutdown command to the script.

I never used step functions but as far as I understand, they are not designed to deal with high latency services such as ec2.

  • thank you for you response.I am using a boto3 so have you any reference code how to ssh from step function (python) – Nimesh Oct 09 '18 at 05:36
  • I don't have any example code but I just found [this](https://stackoverflow.com/questions/42645196/how-to-ssh-and-run-commands-in-ec2-using-boto3) – Hanfbaron Oct 09 '18 at 06:09
  • I need one more help...how do i import .pem file in step function? – Nimesh Oct 09 '18 at 07:14