Skip to main content

AWS - EC2 Instance Start / Stop using AWS Lambda Python

Steps to Setup Lambda to Start / Stop EC2 instances:

1) Create a Lambda Function with 'Author from scratch' and with Function name 'ecStartInstance' and Runtime 'Python 3.8'



2) Choose new role with basic Lambda permission




3) Create policy using IAM role




4) Click on JSON Editor option and update EC2 instance policy json and click on 'Review Policy'


JSON:
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "logs:CreateLogGroup", "logs:CreateLogStream", "logs:PutLogEvents" ], "Resource": "arn:aws:logs:*:*:*" }, { "Effect": "Allow", "Action": [ "ec2:Start*", "ec2:Stop*" ], "Resource": "*" } ] }


5) Give name and description for the policy and click on 'Create Policy'.


6) Edit the IAM role & add IAM Policy to that.



Click on IAM role. Click on 'Attach Policies' and select 'ec_start_stop_lambda' .


7) Write Lambda Function and select 'Configure test event'.



code:

import boto3
region = 'us-east-1'
instances = ['i-0a43e8f5990e24627',]
ec2 = boto3.client('ec2', region_name=region)

def lambda_handler(event, context):
    ec2.start_instances(InstanceIds=instances)
    print('stopped your instances: ' + str(instances))


8) Give event name 'ecStartEvent'


9) Edit basic settings, set timeout as 10 seconds.




10) To test the lambda function, click on 'Test' button.


Thats it.

Comments

Popular posts from this blog

SQL Interview Questions- Oracle - Find top 2 department that contains more employees

Enter into mysql by using below command mysql Step1: Create a database by using below query. mysql> create database nippo; Step 2: List down the databases and check the created database is available or not. mysql> show databases; Step 3: Move within the database using below command. mysql> use nippo; Step 4: Create a table called employee. mysql> create table employee (id int primary key auto_increment, dept_name varchar(255), name varchar(255)); Step 6: Check the given columns and attributes exactly available in the table. mysql> desc employee; Step 5: Insert the values in the table. mysql> insert into employee values(NULL, 'infra', 'mika'); mysql> insert into employee values(NULL, 'infra', 'soomu'); mysql> insert into employee values(NULL, 'infra', 'mulla'); mysql> insert into employee values(NULL, 'support', 'maari'); mysql> insert into employee values(NULL, 

Build docker image with Flask & Python3 and Ubuntu 18.04

Setting Up Flask Assuming docker installed in your ubuntu 18.04 instance. Move into your project path. Example : /home/gisak/projects/python/logics/pic_holdr/pic_holdr/pic_flk_holdr Files present within project folder were below. Contents present within requirements.txt file : flask==1.1.1 Contents present within Dockerfile : FROM ubuntu:18.04 MAINTAINER GisakInc 'support@gisakinc.com' RUN apt-get update -y \ apt-get install -y python3-pip python3-dev COPY ./requirements.txt /app/requirements.txt WORKDIR /app RUN pip3 install -r requirements.txt COPY . /app ENTRYPOINT [ "/usr/bin/python3" ] CMD ["base.py"] Contents present within base.py : from flask import Flask app = Flask(__name__) @app.route('/') def hello_world(): return 'Hello, World!' if __name__ == '__main__': app.run(host='0.0.0.0', port=9302) Creating Virtual Environment with python3 : python3.6 -m virtualen