How KMS Handles the EC2 Server for Start and Stop Using Lambda
Tags: awsec2, awskms, awslambda, awsiam
Published: 2022-06-12 15:36:59

“ I have checked the documents of AWS to resolve the issue of not being able to start the ec2 server from the lambda function. So I checked for various solutions and found out that it's happening because of volume encryption. Adding a lambda role as a key user in CMK resolved my problem. Pricing of Amazon EC2 depends on usage of instance and KMS custom key costs $1 per key.”

AWS Lambda is a serverless compute service that lets you run code without provisioning or managing servers, creating workload-aware cluster scaling logic, maintaining event integrations, or managing runtimes. With Lambda, you can run code for virtually any type of application or backend service - all with zero administration. Just upload your code as a ZIP file or container image, and Lambda automatically and precisely allocates compute execution power and runs your code based on the incoming request or event, for any scale of traffic. You can set up your code to automatically trigger from 140 AWS services or call it directly from any web or mobile app. You can write Lambda functions in your favorite language (Node.js, Python, Go, Java, and more) and use both serverless and container tools, such as AWS SAM or Docker CLI, to build, test, and deploy your functions. To learn more, read the [AWS Lambda](https://docs.aws.amazon.com/lambda/latest/dg/welcome.html).

In this post, you will get to know how the kms handles the ec2 server for start and stop using lambda. Here I have used an ec2 server with encrypted volume and a custom managed key which is able to add a key user as a lambda role to start and stop ec2 server using lambda run.

#Prerequisites
You’ll need an Amazon EC2 Server for this post. [Getting started with amazon EC2](https://aws.amazon.com/ec2/getting-started/) provides instructions on how to launch an EC2 Server.

You’ll need an AWS Key Management Service for this post. [Getting started with AWS KMS](https://aws.amazon.com/kms/getting-started/) provides instructions on how to create a custom key. For this blog, I assume that I have an ec2 server with encrypted volume and a custom key created.

#Architecture Overview
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ge6xa0jxzurvx5t0b3py.png)
The architecture diagram shows the overall deployment architecture with data flow, amazon ec2, amazon elastic block store volume, aws kms and aws lambda.

#Solution overview
The blog post consists of the following phases:
1. Create IAM Policy and Role for Lambda Execution  
2. Create of Lambda Function for Start and Stop of EC2 Server
3. Testing of EC2 Server Start and Stop with Cloudwatch Logs

I have a custom key and a ec2 server as below →
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2sp22l0p3gfur8yhx010.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jxfz841vf43r17w3l6ik.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jdcwi8ii4a2z48y36vqz.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bi7oqis41f1q6leppx3q.png)

##Phase 1: Create IAM Policy and Role for Lambda Execution  
1. Open the IAM console and create an IAM policy for cloudwatch logs and ec2. Also create an IAM role for lambda function of ec2 server execution.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a8g3dc0p4euirl2e5by1.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l7rtr1oirpb8ayz1xa6l.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cv2i64q63vd5mptlkk5i.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c2503geibhcc5e9z78tx.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sunsv76wnqolbitn7u55.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9dh5jqv1xjchk2y05g21.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2hsc2b5fkxrbkw9nlcqr.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ta07ib4wly5qkkj7q55h.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2koy8j14u9su79erf1km.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8e4bhjuqgwpl81fxu87f.png)

##Phase 2: Create of Lambda Function for Start and Stop of EC2 Server 
1. Open the lambda console and create a function for start and stop of the ec2 server with required parameters of set timeout.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5vw6rkye4rlooh7189p3.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zyl6q0ld9thfw4falnzf.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2ds9mtvtxf3rruiondcs.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ly897ydz9q35xdes8bbv.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/usrysijby9onqytg3l0t.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uav45znz8sn3o8x49gy0.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aqahxx087jrs5wtsfosn.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y9cji0uaxnr5xjb23d7m.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hkpt8988id9jgcskk2nw.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i1wg4fraiiee4ovmjo1e.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3kgxwna0muodcebwngdx.png)

##Phase 3: Testing of EC2 Server Start and Stop with Cloudwatch Logs
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gsazk48m55ivm8pimkov.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g5p7df1yay2ew85p27gn.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pix5eapsineryubg0ffm.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tfos9vkv4azyul85whqy.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/598tbhn55a7g6cgjbcyd.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/47zpxpl8lorbzyxdqy33.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/61mj8ken21oe1tj5nn1o.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wr6kkld2mmi0c7o5xz9t.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g38yn2rhje9fgdca8o7r.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fiqlw6pipplfwdmemu15.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9na8nxkst73pa3imlrk5.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n0q04vvfgdm2b16ktq06.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/arofdyyt2umw98u93675.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fdrs65uhjzqek0h17nia.png)

#Clean-up
Delete EC2 Instance, EC2 Volume, KMS Custom Key, IAM Role, IAM Policy, Lambda.

#Pricing
I review the pricing and estimated cost of this example. 

Cost of EC2 Instance = $0.01

Cost of Key Management Service = $1.0

Cost of Data Transfer = $0.0

Cost of Lambda = $0.0

Cost of Cloudwatch = $0.0 

Total Cost = $(0.01 + 1.0 + 0.0 + 0.0 + 0.0) = $1.01

#Summary
In this post, I showed “how KMS handles the EC2 server for start and stop using lambda”.

For more details on AWS Lambda, Checkout Get started AWS Lambda, open the [AWS Lambda console](https://ap-south-1.console.aws.amazon.com/lambda/home?region=ap-south-1#/discover). To learn more, read the [AWS Lambda documentation](https://docs.aws.amazon.com/lambda/?id=docs_gateway).

Thanks for reading!

Connect with me: [Linkedin](https://www.linkedin.com/in/gargee-bhatnagar-6b7223114)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/38sdi4nsp89fnui8h7dk.png)