![](https://it-infrastructure.solutions/content/images/2021/08/under_construction.jpg)
This article has not been completed yet. However, it may already contain helpful information and therefore it has been published at this stage.
Create an EC2 - Instance....
Create IAM Policy & IAM Role
![](https://it-infrastructure.solutions/content/images/2021/09/image-278.png)
![](https://it-infrastructure.solutions/content/images/2021/09/image-279.png)
![](https://it-infrastructure.solutions/content/images/2021/09/image-280.png)
{
"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": "*"
}
]
}
![](https://it-infrastructure.solutions/content/images/2021/09/image-281.png)
![](https://it-infrastructure.solutions/content/images/2021/09/image-282.png)
Enter a name. For example itinfrasollambdaec2
![](https://it-infrastructure.solutions/content/images/2021/09/image-283.png)
![](https://it-infrastructure.solutions/content/images/2021/09/image-284.png)
![](https://it-infrastructure.solutions/content/images/2021/09/image-285.png)
![](https://it-infrastructure.solutions/content/images/2021/09/image-286.png)
![](https://it-infrastructure.solutions/content/images/2021/09/image-287.png)
![](https://it-infrastructure.solutions/content/images/2021/09/image-289.png)
![](https://it-infrastructure.solutions/content/images/2021/09/image-290.png)
![](https://it-infrastructure.solutions/content/images/2021/09/image-291.png)
![](https://it-infrastructure.solutions/content/images/2021/09/image-292.png)
![](https://it-infrastructure.solutions/content/images/2021/09/image-293.png)
Create a Lambda function
![](https://it-infrastructure.solutions/content/images/2021/09/image-271.png)
![](https://it-infrastructure.solutions/content/images/2021/09/image-272.png)
![](https://it-infrastructure.solutions/content/images/2021/09/image-294.png)
![](https://it-infrastructure.solutions/content/images/2021/09/image-296.png)
![](https://it-infrastructure.solutions/content/images/2021/09/image-297.png)
import boto3
region = 'eu-central-1'
instances = ['i-0bf45f32ee3047d76']
ec2 = boto3.client('ec2', region_name=region)
def lambda_handler(event, context):
ec2.stop_instances(InstanceIds=instances)
print('stopped your instances: ' + str(instances))
![](https://it-infrastructure.solutions/content/images/2021/09/image-298.png)
![](https://it-infrastructure.solutions/content/images/2021/09/image-299.png)
![](https://it-infrastructure.solutions/content/images/2021/09/image-300.png)
![](https://it-infrastructure.solutions/content/images/2021/09/image-301.png)
![](https://it-infrastructure.solutions/content/images/2021/09/image-302.png)
![](https://it-infrastructure.solutions/content/images/2021/09/image-277.png)
import boto3
region = 'eu-central-1'
instances = ['i-0bf45f32ee3047d76']
ec2 = boto3.client('ec2', region_name=region)
def lambda_handler(event, context):
ec2.start_instances(InstanceIds=instances)
print('started your instances: ' + str(instances))
Sources:
https://it-infrastructure.solutions/spinning-up-your-first-ec2-instance-aws/
Tutorial: Schedule AWS Lambda functions using EventBridge - Amazon EventBridge
Learn how to use Amazon EventBridge to schedule a Lambda function.
https://aws.amazon.com/de/premiumsupport/knowledge-center/start-stop-lambda-cloudwatch/