aws ec2 import-image via Terraform - amazon-web-services

I'm searching for the Terraform equivalent of "aws ec2 import-image --description "Windows 2008 OVA" --license-type --disk-containers file://containers.json" but cannot find a matching resource command in the documentation.
The purpose is to lift an OVA image out of S3 and covert to AMI so it can be used to launch EC2 instances.

There is an option in packer where you can create OVA by a builder and upload it to S3 and then create an AMI. Once you have the AMI, you can inject into your terraform script to launch it.
https://www.packer.io/docs/post-processors/amazon-import.html

https://github.com/terraform-providers/terraform-provider-aws/issues/5998
Right now terraform not supporting for ec2 image import

Related

How to find the created date of the AWS Lightsail Instance?

I have like 15 Lightsail instances created on my AWS account and now I wanted to know when these Lightsail instances were created.
The creation date of the Lightsail instances on which date and time these were created. But is not able to find this information from the AWS Lightsail console.
You can use the AWS Command Line Interface (AWS CLI) to retrieve the creation date of your Lightsail instances. The following AWS CLI command will list all of your Lightsail instances and the creation date for each instance:
aws lightsail get-instances --query "instances[*].{Name:name, CreationDate:createdAt}"
This command uses the get-instances command to retrieve information about all of your Lightsail instances, and the --query option to extract the name and creation date of each instance.
Alternatively, you can also retrieve the creation date of a specific Lightsail instance by using the get-instance command and specifying the instance name:
aws lightsail get-instance --instance-name <instance_name> --query "instance.createdAt"
Replace <instance_name> with the name of the Lightsail instance you want to retrieve information for.
Note: The AWS CLI must be installed and configured on your local machine in order to use these commands.
If you haven't already done so, just follow these steps:
1- Install the AWS CLI:
You can install the AWS CLI on your local machine by following the installation instructions for your operating system. You can find the installation instructions at the following URL: https://aws.amazon.com/cli/
2- Configure the AWS CLI:
After installing the AWS CLI, you need to configure it with your AWS credentials. You can do this by running the following command:
aws configure
This will prompt you for your AWS access key ID, secret access key, default region name, and default output format. You can find your AWS access keys in the AWS Management Console.
3- Verify the configuration:
To verify that your AWS CLI is configured correctly, you can run the following command:
aws lightsail get-instances
This command should list all of your Lightsail instances in your AWS account.
With the AWS CLI installed and configured, you can now use the AWS CLI commands to retrieve information about your Lightsail instances, including the creation date.
I hope my answer helps you

How to create AMI of my EC2 instance through API

I want to automatically create an AMI for my EC2 instance through a cron job being executed at my node server.Is there any aws API which can help me in creating that?
I googled my way till here.But I am getting the following error when I trynna hit
'https://ec2.amazonaws.com/?Action=CreateImage&Description=demo-server-new-ami&InstanceId=i-test&Name=Demo-Server-through-api&NoReboot=true'.
I am open to implement any other solution for automatically generating the AMI at egular intervals.
Error:
<?xml version="1.0" encoding="UTF-8"?>
<Response><Errors><Error><Code>InvalidAction</Code><Message>The action CreateImage is not valid for this web service.</Message></Error></Errors><RequestID>5ac466db-5ab0-433b-8696-5564ab78f52e</RequestID></Response>
To Create AMI/Image from your EC2 instance, You can use AWS CLi command.
Example:
aws ec2 create-image --instance-id <INSTANCE_ID> --name "My server" --description "An AMI for my server" --region <REGIOn>
Reference: https://docs.aws.amazon.com/cli/latest/reference/ec2/create-image.html
I hope this helps!

Is it possible to replace AMI programmatically in AWS ElasticBeanstalk?

From the console, I am able to replace the AMI under Configuration > Modify Instances > AMI ID
I'm trying to do this programmatically using AWS CLI but couldn't find any API to do this.
I tried to replace AMI ID from Cloudformation (that EB deploys) to replace ASG and LC, but then EB environment broke.
I found it, using this command:
aws elasticbeanstalk update-environment --application-name $EBAppName --environment-name $EBEnvName --option-settings Namespace=aws:autoscaling:launchconfiguration,OptionName=ImageId,Value=$ImageID

Is it possible to launch one of my custom AWS AMI using CLI?

Basically, what I am looking for is the CLI version of http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/launching-instance.html
Previously I have saved my instance as EBS AMI, and want to launch it using command line preferably, or APIs, instead of using Web Interface.
Thank you.
Here is an example of launching a new Amazon EC2 instance via the AWS Command-Line Interface (CLI) with a specified Amazon Machine Image (AMI):
aws ec2 run-instances --image-id ami-abcd1234 --security-group-id sg-abcd1234 --instance-type t1.micro
See: AWS CLI run-instances documentation

How to run a script on an EC2 instance remotely?

I have an EC2 instance and I need to download a file from its D drive through my program. Currently, it's a very annoying process because I can't access the instance directly from my local machine. The way what I am doing now is running a script on the instance and the instance uploads the file I need to S3 and my program access S3 to read the file.
Just wonder whether there is any simple way to access the drive on the instance instead of going through S3?
I have used AWS DataPipeline and its task runner to execute scripts on a remote instance. The taskrunner waits for a pipeline event published to its worker group.
http://docs.aws.amazon.com/datapipeline/latest/DeveloperGuide/dp-using-task-runner.html
I use it to execute shell script and commands on a schedule. The script to run should be uploaded to S3, and the Data pipeline template specifies the script's path. Works great for periodic tasks. You can do anything you want on the remote box via the script.
You cannot directly download the file from EC2, but via s3( or maybe using scp command) from your remote ec2.
But to simplify this annoying process you can use AWS Systems Manager.
AWS Systems Manager Run Command allows you to remotely and securely run set of commands on EC2 as well on-premise server. Below are high-level steps to achieve this.
Attach Instance IAM role:
The ec2 instance must have IAM role with policy AmazonSSMFullAccess. This role enables the instance to communicate with the Systems Manager API.
Install SSM Agent:
The EC2 instance must have SSM agent installed on it. The SSM Agent process the run command requests & configure the instance as per command.
Execute command :
Example usage via AWS CLI:
Execute the following command to retrieve the services running on the instance. Replace Instance-ID with ec2 instance id.
aws ssm send-command --document-name "AWS-RunShellScript" --comment "listing services" --instance-ids "Instance-ID" --parameters commands="service --status-all" --region us-west-2 --output text
More detailed information: https://www.justdocloud.com/2018/04/01/run-commands-remotely-ec2-instances/