Run exe file on EC2 from AWS Lambda - amazon-web-services

I have a requirement to run a .exe file with the SQS event message as parameter. The .exe is installed on EC2. Is it possible to invoke the exe from the AWS Lamda function?

The best way to run scripts on EC2 instances from outside of the EC2 instance is probably to use AWS Systems Manager Run Command.
The setup is fairly simple:
your EC2 instance needs to include the SSM Agent (it probably already does if it was launched from an Amazon-provided Amazon Linux or Ubuntu AMI)
your EC2 instance needs to be launched with an IAM role with an attached AmazonEC2RoleforSSM policy, see here
your Lambda function needs an IAM role allowing it to invoke Run Command (the action is ssm:SendCommand)
Alternatively, you could do this in other ways, for example:
use Fabric or Paramiko to exec commands over SSH
install a web server on the EC2 instance that allows you to remotely invoke an API to execute the script

Related

AWS - Conditionally run a script on EC2 instances

I am looking for a way to conditionally run a script on every existing / new EC2 instances.
For example, in Azure, you can create an Azure Policy that is executed on every existing / new VM, and when a set of conditions apply on that VM, you can deploy a VM extension or run a DSC script.
I am looking for the equivalent service in AWS.
From AWS Systems Manager Run Command - AWS Systems Manager:
Using Run Command, a capability of AWS Systems Manager, you can remotely and securely manage the configuration of your managed instances. A managed instance is any Amazon Elastic Compute Cloud (Amazon EC2) instance or on-premises machine in your hybrid environment that has been configured for Systems Manager. Run Command allows you to automate common administrative tasks and perform one-time configuration changes at scale. You can use Run Command from the AWS Management Console, the AWS Command Line Interface (AWS CLI), AWS Tools for Windows PowerShell, or the AWS SDKs.
Administrators use Run Command to perform the following types of tasks on their managed instances: install or bootstrap applications, build a deployment pipeline, capture log files when an instance is removed from an Auto Scaling group, and join instances to a Windows domain.
You will need to trigger the Run Command to execute on nominated EC2 instances. It will not automatically run for every 'new' instance.
Alternatively, there is Evaluating Resources with AWS Config Rules - AWS Config:
Use AWS Config to evaluate the configuration settings of your AWS resources. You do this by creating AWS Config rules, which represent your ideal configuration settings. While AWS Config continuously tracks the configuration changes that occur among your resources, it checks whether these changes violate any of the conditions in your rules. If a resource violates a rule, AWS Config flags the resource and the rule as noncompliant.
For example, when an EC2 volume is created, AWS Config can evaluate the volume against a rule that requires volumes to be encrypted. If the volume is not encrypted, AWS Config flags the volume and the rule as noncompliant. AWS Config can also check all of your resources for account-wide requirements. For example, AWS Config can check whether the number of EC2 volumes in an account stays within a desired total, or whether an account uses AWS CloudTrail for logging.
You can create an AWS Config custom rule that triggers a process when a non-compliant resource is found. This way, an automated action could correct the situation.
You can also use an AWS managed service such as OpsWorks (Managed Chef/Puppet).
This can give you a way of running the commands in an organized way by allowing you to create defined sets of instances and associated resources.

Run a batch file on EC2 from a (python) lambda

I can see a generic way of starting an EC2 from lambda in Start and Stop Instances at Scheduled Intervals Using Lambda and CloudWatch.
Suppose I use that method to start an EC2, and suppose the AMI is a windows server 2019 customised to have a .bat file on the desktop, and also suppose I'm using a python lambda.
How can I execute this batch file from the lambda? (i.e. just as though someone had RDP'd into the instance and double-clicked on it)
Note: To be very clear, basically I want to start the EC2 using the method given in the AWS docs (above), and right after the instance has started, to run the batch file that will be sitting on the instance's desktop
I think you have a few concepts mixed together.
AWS Lambda functions run on the Lambda service, without having to use Amazon EC2 instances. This is what makes them "serverless".
If you have a batch file on an Amazon EC2 instance, you would presumably want to run that batch file on the EC2 instance itself, without involving Lambda (since you have got a server).
If you wish to run a script on an EC2 instance when it launches for the first time, you can provide a PowerShell or Command-Line script via the User Data field. Software on the AMI will automatically execute this script the first time that the instance starts.
This script could do all the work itself, or it could simply call another script that is stored on the disk. Some people use the script to download another script from a repository (eg Amazon S3 or GitHub) and then execute the downloaded script.
For more information, see: Running Commands on Your Windows Instance at Launch - Amazon Elastic Compute Cloud
If the Amazon EC2 instance is already running and you wish to trigger a script to execute, you can use the AWS Systems Manager Run Command. This works by having an agent on the instance which can be remotely triggered, thereby running scripts without having to login to the instance.

User-data script doens't launch with EC2 Instance

Background:
Services used: ec2, autoscaling, s3, sqs, cloudwatch
AMI and Environement: Windows 64-bit
Network: IAM and security group attached
Job: Run a script which starts a program (.exe) which is loaded from S3
I have an auto scale option that launches a N number of Instances. The user data script is based on aws CLI and few commands in powershell. I was expecting the instances to execute my script upon their initialization. Note that some of the tasks before the Job is to first download the aws CLI using powershell, because the rest of the script is based on aws commands
What am I missing ? I thought the launch should start the script in user-data.
Note that this script was tested on an instance with the same configurations (VPC, Security Group, etc..)

Amazon EC2 Instance - Where to add the credentials file?

I have an Amazon EC2 Instance. On my Instance I want to run a python program. The program tries to access other amazon services. On my Mac the program worked fine, because the credentials file is in the folder "~/.aws/credentials". But I don't know where on my amazon EC2 instance the library "~" is. So where do I have to put my credentials? Hope you can help me.
~ is a shortcut for your home directory. Just go into that directory using the command cd ~. This is the directory you will always start in when you SSH into your server.
Alternatively you could install the AWS CLI and run aws configure. If you are using Amazon Linux then the AWS CLI tool is already installed.
Also, storing credentials on your EC2 instance is not the preferred method. You should look into using EC2 instance profiles.

AWS- Set user data for a running instance

I have a running AWS instance. I want to set user-data for this instance.
Question is how do I do it either using AWS console or using AWS CLI tools.
You have to stop your instance to change the user-data. The AWS EC2 User Guide has instructions on how to do it: http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/Stop_Start.html#Using_ChangingAttributesWhileInstanceStopped