<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Carl Hayes</title>
    <description>The latest articles on Forem by Carl Hayes (@carlghayes).</description>
    <link>https://forem.com/carlghayes</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F914933%2F5d739eac-390f-4e8c-87b8-baea8cc9eca8.png</url>
      <title>Forem: Carl Hayes</title>
      <link>https://forem.com/carlghayes</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/carlghayes"/>
    <language>en</language>
    <item>
      <title>Using Lambda and EventBridge to automatically stop and start EC2 Instances</title>
      <dc:creator>Carl Hayes</dc:creator>
      <pubDate>Sun, 04 Dec 2022 23:36:32 +0000</pubDate>
      <link>https://forem.com/aws-builders/using-lambda-and-eventbridge-to-automatically-stop-and-start-ec2-instances-5d04</link>
      <guid>https://forem.com/aws-builders/using-lambda-and-eventbridge-to-automatically-stop-and-start-ec2-instances-5d04</guid>
      <description>&lt;h2&gt;
  
  
  &lt;u&gt;So What's the Big Picture?&lt;/u&gt;
&lt;/h2&gt;

&lt;p&gt;When looking at AWS cloud consumption, many companies may have instances that aren't actively utilized compared to others. Wouldn't it be cool, if there was a way to shutdown those instances outside of work hours, and start them back up before people log onto the system?&lt;/p&gt;

&lt;p&gt;Well, you're in luck, because today I want to show you how to save money on your monthly AWS bill, by creating an automatic stop and start schedule on your infrequently used ec2 instances.&lt;/p&gt;




&lt;p&gt;The services involved in making this happen are:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;AWS IAM&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;AWS Identity and Access Management (IAM) is a web service that allows you to securely control access to your AWS resources. With IAM, we can govern who can sign in, and authorization to use resources.&lt;/p&gt;

&lt;p&gt;For more about IAM, click this &lt;a href="https://docs.aws.amazon.com/IAM/latest/UserGuide/introduction.html" rel="noopener noreferrer"&gt;link&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;AWS Lambda&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;AWS Lambda is a compute service that allows you to build applications that respond to new information and events.&lt;/p&gt;

&lt;p&gt;Visit this &lt;a href="https://docs.aws.amazon.com/lambda/latest/dg/welcome.html" rel="noopener noreferrer"&gt;link&lt;/a&gt; for more information about Lambda.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;AWS Eventbridge&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;EventBridge is a serverless event bus that ingests data from your apps, AWS services and routes that data to targets.&lt;/p&gt;

&lt;p&gt;Visit this &lt;a href="https://docs.aws.amazon.com/eventbridge/" rel="noopener noreferrer"&gt;link&lt;/a&gt; to learn more about EventBridge&lt;/p&gt;




&lt;p&gt;Prior to getting started, It's important that you know which ec2 instances you're wanting to test. For the sake of the demonstration, I am conducting the shutdown &amp;amp; start up by collecting the instance IDs.&lt;/p&gt;

&lt;p&gt;For the demo, only one region will be used, but if you're interested in running it across multiple regions, you may need to create another lambda function to associate with that specified region.&lt;/p&gt;

&lt;p&gt;Now that the formalities are out of the way, lets get to it!&lt;/p&gt;




&lt;h3&gt;
  
  
  Create IAM Policy
&lt;/h3&gt;

&lt;p&gt;First, search for &lt;strong&gt;IAM&lt;/strong&gt; in the top search bar, select create policy, and choose &lt;strong&gt;JSON&lt;/strong&gt;. For ease of use, please copy and paste the code provided.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "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": "*"
    }
  ]
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can name your policy whatever you desire, but for nostalgic purposes, I named my policy &lt;strong&gt;StopNGo&lt;/strong&gt; , which was a gas station in Houston. &lt;/p&gt;

&lt;p&gt;After the policy is named, select &lt;strong&gt;create&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Developing the IAM Role
&lt;/h2&gt;

&lt;p&gt;Next, we want to create the IAM Role for the Lambda function.&lt;/p&gt;

&lt;p&gt;Since we are still in the &lt;strong&gt;IAM console&lt;/strong&gt;, select &lt;strong&gt;Roles&lt;/strong&gt; and select &lt;strong&gt;Create Roles&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Select the &lt;strong&gt;AWS Service&lt;/strong&gt;, and choose &lt;strong&gt;Lambda&lt;/strong&gt; as the common use case.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foydsv49vfku6f2mu9duf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foydsv49vfku6f2mu9duf.png" alt="Choose-AWS-Service"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Select Next, and search for &lt;strong&gt;AWSLambdaBasicExecutionRole&lt;/strong&gt; and choose it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fncpyyqir65wk8qb2fz2m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fncpyyqir65wk8qb2fz2m.png" alt="IAM Role"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;While we are at it, lets ensure to add the policy we previously created. Please be sure to clear the existing filter for Lambda prior to searching.&lt;/p&gt;

&lt;p&gt;Select &lt;strong&gt;Next&lt;/strong&gt;, and label your role, and click &lt;strong&gt;create role&lt;/strong&gt;. I kept it similar and named it StopNGo-N.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fai5n6s7pyfi3v5vas5r7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fai5n6s7pyfi3v5vas5r7.png" alt="Name IAM Role"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating our Lambda Function
&lt;/h2&gt;

&lt;p&gt;At this point, we need to create a lambda function that will end up triggering the shut down and start of our selected ec2 instances.&lt;/p&gt;

&lt;p&gt;Let's utilize the search bar at the top, and type &lt;strong&gt;Lambda&lt;/strong&gt;, choose it, and to get started choose &lt;strong&gt;Create a function&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvwyr65wknocfepaplpdc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvwyr65wknocfepaplpdc.png" alt="Lambda Setup-1"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Ensure the &lt;strong&gt;Author from scratch&lt;/strong&gt; option is highlighted, and proceed to fill out&lt;/p&gt;

&lt;p&gt;Function Name : StopNGo-Stop&lt;br&gt;
Runtime : Python 3.9&lt;br&gt;
Architecture : X86_64&lt;/p&gt;

&lt;p&gt;Under the &lt;strong&gt;Permissions&lt;/strong&gt; section, expand the &lt;strong&gt;Change default execution role&lt;/strong&gt; and choose &lt;strong&gt;Use an existing role&lt;/strong&gt;. Be sure to attach the previous IAM role created.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fspewbnqc4sjxtchqwdak.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fspewbnqc4sjxtchqwdak.png" alt="Lambda Function Setup"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Select &lt;strong&gt;Create Function&lt;/strong&gt;, and let the real work commence!&lt;/p&gt;


&lt;h2&gt;
  
  
  Function Code
&lt;/h2&gt;

&lt;p&gt;Under the &lt;strong&gt;code&lt;/strong&gt; tab, make sure we are in the &lt;strong&gt;code source&lt;/strong&gt; subtask and lets change what we have in the editor.&lt;/p&gt;

&lt;p&gt;Feel free to copy and paste the code below into your editor pane, but modify the &lt;strong&gt;region&lt;/strong&gt; and &lt;strong&gt;instances&lt;/strong&gt; section to reflect your environment.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import boto3
region = 'us-east-1'
instances = ['i-0d274fa2a0606dd64', 'i-0081fec0e3aa3883a', 'i-05ac19a60ca8e823d', 'i-053147387fd6e625e']
ec2 = boto3.client('ec2', region_name=region)

def lambda_handler(event, context):
    ec2.stop_instances(InstanceIds=instances)
    print('We stopped your instances: ' + str(instances))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Save&lt;/strong&gt; the function and &lt;strong&gt;deploy&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now we want to create a separate function to kick-start our instances. When you create a lambda function, you can copy and paste the code block below. Please be sure to modify the region and instance ids to fit your use case.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import boto3
region = 'us-east-1'
instances = ['i-0d274fa2a0606dd64', 'i-0081fec0e3aa3883a', 'i-05ac19a60ca8e823d', 'i-053147387fd6e625e']
ec2 = boto3.client('ec2', region_name=region)

def lambda_handler(event, context):
    ec2.start_instances(InstanceIds=instances)
    print('We started your instances: ' + str(instances))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Save&lt;/strong&gt; the function and &lt;strong&gt;deploy&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Testing our functions
&lt;/h2&gt;

&lt;p&gt;After creating our functions, we want to ensure they work.&lt;/p&gt;

&lt;p&gt;Select our &lt;strong&gt;StopNGo-Stop&lt;/strong&gt; function&lt;br&gt;
Choose the &lt;strong&gt;Code&lt;/strong&gt; tab&lt;br&gt;
In the &lt;strong&gt;code source&lt;/strong&gt; section, click &lt;strong&gt;Test&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flavxe5dmh3yjtba3kktc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flavxe5dmh3yjtba3kktc.png" alt="Lambda-test"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the &lt;strong&gt;Configure test&lt;/strong&gt; pop-up option, &lt;strong&gt;create a new event&lt;/strong&gt;, enter a suitable name, and click &lt;strong&gt;Save&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd1pajyhxrvb4pgjuc3ml.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd1pajyhxrvb4pgjuc3ml.png" alt="Test-Event"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Farlbwelycc6kivnewyah.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Farlbwelycc6kivnewyah.png" alt="lambda-save"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Sidenote&lt;/strong&gt;: There is no need to modify the JSON code for our test event, because the function won't use it.&lt;/p&gt;

&lt;p&gt;Now, lets press the &lt;strong&gt;Test&lt;/strong&gt; button to run the function.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fozzr4husmxrkq9e0rmy8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fozzr4husmxrkq9e0rmy8.png" alt="Success-fail"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If we analyze the execution results, the function ran properly and stopped the targeted instances. &lt;/p&gt;

&lt;p&gt;So, let's check our EC2 instances. The test should've shut them down.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff7zfhl1itybdxr8ggf4q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff7zfhl1itybdxr8ggf4q.png" alt="Success-Lambda"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Regarding the start function, we will mirror the previous steps used for our stop function. &lt;/p&gt;

&lt;p&gt;Once you have verified its success, we are one step closer to the finish line.&lt;/p&gt;




&lt;h2&gt;
  
  
  Scheduling the Time Schedule with EventBridge
&lt;/h2&gt;

&lt;p&gt;Here is where the rubber meets the road, we will be creating a rule that stops and starts the targeted instances at a defined time.&lt;/p&gt;

&lt;p&gt;Search for &lt;strong&gt;EventBridge&lt;/strong&gt;, and open the console.&lt;/p&gt;

&lt;p&gt;Select &lt;strong&gt;Create Rule&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhn4kn0hf60fz405xtnyu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhn4kn0hf60fz405xtnyu.png" alt="EventBridge-1"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Name the Rule&lt;br&gt;
Write the &lt;strong&gt;description&lt;/strong&gt; - Optional&lt;br&gt;
Under &lt;strong&gt;Rule Type&lt;/strong&gt;, select &lt;strong&gt;Schedule&lt;/strong&gt;&lt;br&gt;
Select &lt;strong&gt;Continue to create rule&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fahfu6y089bcmp15yctbh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fahfu6y089bcmp15yctbh.png" alt="EventBridge-2"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For the schedule pattern, we are going to utilize the &lt;strong&gt;fine-grained&lt;/strong&gt; schedule, as this is a suitable use case for our example.&lt;/p&gt;

&lt;p&gt;Next, we are going to define the parameters that our &lt;strong&gt;cron expression&lt;/strong&gt; will trigger.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F10n0jg13fe77wq3i4oju.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F10n0jg13fe77wq3i4oju.png" alt="eventBridge-3"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I want to stop my ec2 instances everyday at 7:30pm UTC,so my minutes and hours will be entered, but different wildcard expressions are used in the other fields.&lt;/p&gt;

&lt;p&gt;If you're wanting to create your own cron expressions, select this &lt;a href="https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/ScheduledEvents.html" rel="noopener noreferrer"&gt;link&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Select &lt;strong&gt;Next&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now, let's define our Target&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Target Types: &lt;strong&gt;AWS Service&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Target: &lt;strong&gt;Lambda Function&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Function: &lt;strong&gt;StopNGo-Stop&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fox0qr6wvddyrad7s3w19.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fox0qr6wvddyrad7s3w19.png" alt="Target-select"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Select Next, skip the tags, and review the rules. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft88lczkuzw1k8stlilkc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft88lczkuzw1k8stlilkc.png" alt="start-bridge"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once rules are created for &lt;strong&gt;StopNGo-Stop&lt;/strong&gt;, we will create another EventBridge rule for our ec2 instances to start at 6:30am UTC. &lt;/p&gt;

&lt;p&gt;Similar to the stop rule, I will modify the cron expression to fit my use case.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fteupr2hjr9sl339bymcg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fteupr2hjr9sl339bymcg.png" alt="EventBridge-Start"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you've been able to followed me up to this point, there will be two EventBridge rules that are similar to the photo below. &lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzdkpdxq7g160se2ehf9m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzdkpdxq7g160se2ehf9m.png" alt="eventbridge-5"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Which means a congratulations is in order, because you've successfully created a lambda function, and created a schedule with EventBridge that stops instances at 7:30pm UTC, and starts them at 6:30am UTC.&lt;/p&gt;

</description>
      <category>devops</category>
      <category>aws</category>
      <category>serverless</category>
      <category>python</category>
    </item>
  </channel>
</rss>
