AWS Ec2 Instance
AWS Ec2 Instance
==========================================================
Ec2
=====
Under aws resources ec2 belongs to compute.
It's a regional service.
It is not managed by AWS, it means we can manage.
Ec2 purchase plans:
===============
1. On demand
it's the default one.
2. Spot Request
according to the bidding action it means requesting an amount.
3. Reserved instance
before purchasing we can select how much cpu and instance type
in terms of specific years we can buy
Instance states
============
1. Terminated
2. Pending
3. Running
4. Stopped
5. initializing
6.here we can select key pair, here we can create new key pair and select it
7.here Click on edit in network settings—>here we can select custom or default vpc—> here we
can select the subnet
Create policy
Managed police
This policies created and managed by AWS
Custom policy
This policies are managed by the users
Inline policy
we will assign the policy for the single user and we can't re use this policy again
to another user
Action:Resource/service
Effect:Allow/deny
Resource:
Condition:
Principal:
}
1. Click on policies—> and then click on create policy
3.Here we can give a policy name and tags for that policy—> then click on create policy.
Next create role
The purpose of the role is used to connect from one service to another service
3. Select the use case for example in my case IAM using Lambda
4. Here we can add permissions/policies for that role here i am created policy already so i am
chose custom policy
5.After that we can give Role name and tags for that Role
After that create lambda function
6. Under Code, Code source, copy and paste the following code. This code stops the EC2 instances
Example function code—stopping EC2 instances
import boto3
region = 'us-west-1'
instances = ['i-12345cb6de4f78g9h']
ec2 = boto3.client('ec2', region_name=region)
def lambda_handler(event, context):
ec2.stop_instances(InstanceIds=instances)
print('stopped your instances: ' + str(instances))
enter a different Function name you used before For example, "StartEC2Instances".
copy and paste the following code.
import boto3
region = 'us-west-1'
instances = ['i-12345cb6de4f78g9h']
ec2 = boto3.client('ec2', region_name=region)
note:region and instances , use the same that you used for the code to stop your EC2
instances.
Test your Lambda functions
==============================
In the Lambda Function, choose Functions.
Choose one of the functions that you created.
Cron expression: enter a time that tells Lambda when to stop your instances.
Here cron expression are in UTC so to change your preferred time zone
\
5.here we select the targets and then click configure details in my case i am chose lambda
6. Here give proffer name and click create role, role will be created
7. Repeat above steps to create a rule to start your EC2 instances.
In non-business hours we don't want to run applications that we make automatically to stop
and start using lambda function.By using this we can optimize the cost.