<?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>DEV Community: José Paiva</title>
    <description>The latest articles on DEV Community by José Paiva (@josevictorps).</description>
    <link>https://dev.to/josevictorps</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%2F779693%2F296b2115-6111-47c2-b904-72689f7c9de6.jpg</url>
      <title>DEV Community: José Paiva</title>
      <link>https://dev.to/josevictorps</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/josevictorps"/>
    <language>en</language>
    <item>
      <title>Setting up a virtual machine to run a scheduled job - Part 1 - Terraform, Serverless Framework and AWS</title>
      <dc:creator>José Paiva</dc:creator>
      <pubDate>Mon, 04 Apr 2022 23:54:09 +0000</pubDate>
      <link>https://dev.to/vaivoa/setting-up-a-virtual-machine-to-run-a-scheduled-job-part-1-terraform-serverless-framework-and-aws-o43</link>
      <guid>https://dev.to/vaivoa/setting-up-a-virtual-machine-to-run-a-scheduled-job-part-1-terraform-serverless-framework-and-aws-o43</guid>
      <description>&lt;p&gt;Hello, I'm José Silva, backend developer at Vaivoa, and today I'm going to teach you how to set up a ec2 machine at aws to run a cron job on a specific schedule with terraform and the serverless framework.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;This article is the first of a series, which in the next steps, I'll explain how to use ansible to configure an ec2 instance dynamically to run a cron job and also configure git to always update the code before running the job.&lt;/b&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Terraform is a IaC tool, which helps you provide your cloud infrastructure with code. it's useful for versioning your infrastructure, documenting and automating your work.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;EC2 is the AWS service for virtual machines. There's a free tier available, so don't worry about expenses(but still remember deleting all the resources created after the tutorial)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The serverless framework helps you deploy and configure lambda functions. We're going to write our lambda functions in python3.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The full code of the project is here: &lt;a href="https://github.com/Jose-Victor-PS/ec2_cron_job_tutorial" rel="noopener noreferrer"&gt;Github&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Architecture
&lt;/h2&gt;

&lt;p&gt;Next there's a picture representing the workflow:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Ftkxzawqqds0kzstqzoid.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Ftkxzawqqds0kzstqzoid.png" alt="Cron Triggers Lambda to Start EC2 Instance" width="800" height="176"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Since we don't want the instance to be on 24/7, we'll rely on a lambda function to control the time the instance is up and running. The stop process will be taken care of by the instance itself. Since we don't know the time the job will take to execute(and for the most cases we can't predict), I'll encapsulate the job execution in a script that will take care of shutting down the instance once the job is finished.&lt;/p&gt;

&lt;p&gt;For the scope of this part of the series, I'll focus only on starting the instance in a specific schedule.&lt;/p&gt;

&lt;h2&gt;
  
  
  Terraform
&lt;/h2&gt;

&lt;p&gt;Let's start with our repository structure. First, terraform uses the working directory to get all information about your infrastructure. So let's create a directory just to put all the terraform config files. Every &lt;code&gt;terraform&lt;/code&gt; command should be used inside the terraform directory.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tutorial/
    terraform/
        main.tf
        machine.tf
        vars.tf
        outputs.tf
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;inside main.tf, let's put this code:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "3.74.3"
    }
  }
}

provider "aws" {
  region = "your-favorite-region"
  profile = "your-aws-profile"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;In the terraform part of the code, we tell which provider we're using, and which version of the provider we're using. Since I'm using version 3.74.3, if you want to know more about the terraform code i'm using, go to this version of the documentation. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In the provider section, we're telling which region we're going to deploy our infrastructure and what aws profile we're using to authenticate. Your profile is defined under the &lt;code&gt;credentials&lt;/code&gt; file in your &lt;code&gt;.aws/&lt;/code&gt; folder.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now, also in main.tf, we're using the data module to query the amazon linux 2 ami. It's useful because the same ami has different ids in different regions and so you don't have to look for it in the aws console:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;
data "aws_ami" "amazon_linux" {
  most_recent = true

  filter {
    name = "name"
    values = ["amzn2-ami-kernel-5.10-hvm*"]
  }

  filter {
    name = "architecture"
    values = ["x86_64"]
  }

  owners = ["137112412989"]
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The amazon linux is an improved version of linux for aws, is supposed to be stable, safe and have a good performance. It's similar to Red Hat 7, so it uses &lt;code&gt;yum&lt;/code&gt; as a package manager, for example.&lt;/p&gt;

&lt;p&gt;Now let's write the vars.tf file. In this file we're going to set up all the terraform variables in the code. This allows us to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;b&gt;Avoid&lt;/b&gt; hard coded values so it's easier to refactor our code.&lt;/li&gt;
&lt;li&gt;Modularize our environment in case we want to deploy different configurations for development and production, for example.&lt;/li&gt;
&lt;/ul&gt;

&lt;pre&gt;&lt;code&gt;
variable "instance_size" {
  type = map(string)

  default = {
    "test" = "t2.micro"
    "prod" = "t2.medium"
  }
}

variable "network" {
  type = object({
    all_ipv4 = string
    all_ipv6 = string
    all_protocols = string
    all_ports = number
  })

  default = {
    all_ipv4 = "0.0.0.0/0"
    all_ipv6 = "::/0"
    all_protocols = "-1"
    all_ports = 0
  }
}

variable "disk" {
  type = map(number)

  default = {
    "test" = 16
    "prod" = 64
  }
}

variable "ssh_key" {
  type = string
  default = "my-ssh-key"
}

variable "inbound_rules" {
  type = object({
    port = number
    protocol = string
    description = string
  })

  default = {
    port = 22,
    protocol = "tcp",
    description = "Allow SSH"
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Here are the values we're using for the &lt;code&gt;test&lt;/code&gt; environment. Feel free to change them as you want:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;For instance type we're using the free tier, so you don't waste any money in this tutorial(as long as you still have the 750 hours per month to use).&lt;/li&gt;
&lt;li&gt;For disk size, we're using 16 GB because is a reasonable size, but depending on the job you want to run, you might increase or decrease this size.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Next there's the machine.tf code. Here I'm setting up the ec2 machine configuration and the security group configuration. I'll explain each part separately.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;
resource "aws_instance" "tutorial_machine" {
  ami = data.aws_ami.amazon_linux.id
  instance_type = var.instance_size.test
  key_name = var.ssh_key
  security_groups = [aws_security_group.tutorial_sg.name]
  root_block_device {
    volume_size = var.disk.test
  }
  tags = {
    Name = "my_tutorial_machine"
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Here are some explanations:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;aws_instance&lt;/code&gt; keyword means it's an ec2 virtual machine.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;tutorial_machine&lt;/code&gt; is the local name terraform uses to reference the resource in code.&lt;/li&gt;
&lt;li&gt;In the ami attribute, we're referencing the amazon linux 2 ami we queried in the data module in the main.tf file.&lt;/li&gt;
&lt;li&gt;In the tags attribute, we're telling what the actual name of the virtual machine will be.&lt;/li&gt;
&lt;li&gt;In the security groups, we're referencing a security group we're about to define next in the machine.tf file.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here's the rest of the code:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;
resource "aws_security_group" "tutorial_sg" {
  name = "tutorial_sg"
  description = "Tutorial Security Group"

  ingress { #Inbound Rule
    from_port = var.inbound_rules.port
    protocol = var.inbound_rules.protocol
    to_port = var.inbound_rules.port
    cidr_blocks = [var.network.all_ipv4]
    ipv6_cidr_blocks = [var.network.all_ipv6]
    description = var.inbound_rules.description
  }

  egress { # Outbond Rule
    from_port = var.network.all_ports
    protocol = var.network.all_protocols
    to_port = var.network.all_ports
    cidr_blocks = [var.network.all_ipv4]
    ipv6_cidr_blocks = [var.network.all_ipv6]
  }

  tags = {
    Name = "Allow SSH Only"
  }

}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In the console, we can set up the security group or create a new one, if we create a new one, there's the default outbond rules allowing every traffic to the internet. With terraform, the security group has no default rule, so we have to specify everything we want.&lt;/p&gt;

&lt;p&gt;In this security group we're setting two rules:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Allow all network traffic exiting the machine&lt;/li&gt;
&lt;li&gt;Only allow traffic entering the machine through the ssh port number 22 from all hosts, both ipv4 and ipv6.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The 22 port is the default for ssh connections, which is something extremely important when configuring or using a virtual machine. In this part of the series we won't use it, but later we will.&lt;/p&gt;

&lt;p&gt;Feel free to open more ports if your job needs it.&lt;/p&gt;

&lt;p&gt;Finally, we're setting up some outputs to know some basic information about our deployed ec2 instance.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;
output "tutorial_public_dns" {
  value = aws_instance.tutorial_machine.public_dns
}

output "tutorial_instance_id" {
  value = aws_instance.tutorial_machine.id
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;b&gt;Don't forget to change all the variables placeholders for your actual variables, like profile name, ssh key name, etc.&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;Now let's run some &lt;code&gt;terraform&lt;/code&gt; commands:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Run &lt;code&gt;terraform init&lt;/code&gt; inside the terraform folder. This command will install the aws provider runtime terraform relies on to understand what "&lt;code&gt;aws_instance&lt;/code&gt;" or "&lt;code&gt;aws_security_group&lt;/code&gt;" means.&lt;/li&gt;
&lt;li&gt;Then run &lt;code&gt;terraform plan&lt;/code&gt; to get a sample output of what might happen when you actually deploy your infrastructure and verify if there's any sintax errors in your code.&lt;/li&gt;
&lt;li&gt;Now run &lt;code&gt;terraform apply&lt;/code&gt; to deploy your machine. Terraform will show you what will be deployed again and will ask you to confirm your action by typing "yes" in the terminal.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;After confirmation, you can go to your aws console, in the ec2 instances panel, and see your tutorial instance right there up and running!&lt;/p&gt;

&lt;p&gt;Since we don't want our instance to be up and running 24/7, let's stop the instance and move on to the final part of the tutorial.&lt;/p&gt;
&lt;h2&gt;
  
  
  Lambda Functions &amp;amp; Serverless Framework
&lt;/h2&gt;

&lt;p&gt;Now let's add a lambda directory to the tutorials folder and put the python script in it to start the ec2 instance, and the serverless.yml file to configure the lambda function.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tutorial/
    terraform/
        main.tf
        machine.tf
        vars.tf
        outputs.tf
    lambda/
        main.py
        serverless.yml
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;Under the lambda folder, in the main.py file, let's write our python code to start the instance based on the &lt;code&gt;name tag&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;
import boto3


def handler(event, context):
    ec2 = boto3.client("ec2")
    response = ec2.describe_instances(
        Filters=[
            {
                'Name': "tag:Name",
                'Values': [
                    'my_tutorial_machine'
                ]
            }
        ]
    )

    instance_id = response['Reservations'][0]['Instances'][0]['InstanceId']

    response = ec2.start_instances(
        InstanceIds=[
            instance_id
        ]
    )
    print(response)


if __name__ == '__main__':
    handler({}, None)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Make sure the &lt;code&gt;name tag&lt;/code&gt; is the same in both the main.py and  the machine.tf.&lt;/p&gt;

&lt;p&gt;I'm using boto3, which is the AWS SDK for Python3 and can be installed via pip. This SDK allows us to send API calls to aws in a easier way to start, stop, modify ec2 instances and much more.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;start instance&lt;/code&gt; operation relies on the instance id, which is something that changes for every deploy. Since we're using terraform and might deploy and redeploy our virtual machine a couple of times, the instance id will change accordingly, and every time it changes we'll have to adapt our python script. So we're first querying the instance information by name, which is something that doesn't change for every deploy.&lt;/p&gt;

&lt;p&gt;Finally, let's write our serverless.yml, configuring the lambda runtime, stage, service name, aws region and IAM role statements.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;
service: tutorial-initializer

provider:
  name: aws
  stage: development
  runtime: python3.6
  region: your-favorite-region
  iamRoleStatements:
    - Effect: Allow
      Action:
        - ec2:DescribeInstances
        - ec2:StartInstances
      Resource: "*"

functions:
  initializer:
    handler: main.handler
    events:
      - schedule:
          enabled: true
          rate: your-cron-expression
    memorySize: 128
    timeout: 30
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In the &lt;code&gt;iamRoleStatements&lt;/code&gt; we're &lt;b&gt;allowing&lt;/b&gt; our lambda function to perform the &lt;code&gt;StartInstances&lt;/code&gt; and the &lt;code&gt;DescribeInstances&lt;/code&gt; on any ec2 instance.&lt;/p&gt;

&lt;p&gt;Here are some important things to know about the serverless.yml file:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In the &lt;code&gt;functions&lt;/code&gt; part of the configuration, we're saying that the &lt;code&gt;initializer&lt;/code&gt; lambda function will be the &lt;code&gt;handler&lt;/code&gt; function in the &lt;code&gt;main.py&lt;/code&gt; file.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;events&lt;/code&gt; part defines the cron &lt;code&gt;schedule&lt;/code&gt; that will trigger the lambda function once the time is right.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;timeout&lt;/code&gt; defines the maximun time in seconds your lambda function is allowed to run. In this case 30 seconds is more than enough.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;memorySize&lt;/code&gt; defines how much memory your function might have in MB. AWS uses this parameter to bill you(lambda function memory size multiplied by the total time of execution), so try to use a low value in here.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Finally, just run &lt;code&gt;sls deploy&lt;/code&gt; inside your lambda folder and that's it! You just deployed a lambda function to start an EC2 instance based on a cron schedule.&lt;/p&gt;



&lt;ul&gt;
&lt;li&gt;&lt;b&gt;Don't forget to replace the region and cron expression placeholders with your preferred region.&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Also, keep in mind the schedule rate is based on the region time zone where your lambda is deployed. So calculate the difference between you local timezone and the aws region timezone to get the desired result.&lt;/b&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Now you learned how to set up a virtual machine in AWS EC2 using free tier and setting up a lambda function to start the machine based on a cron schedule.&lt;/p&gt;

&lt;p&gt;In the next modules I'll teach about configuring the machine with ansible, setting up the cron job to start on boot and updating the code with git.&lt;/p&gt;

&lt;p&gt;The full code of the project is here: &lt;a href="https://github.com/Jose-Victor-PS/ec2_cron_job_tutorial" rel="noopener noreferrer"&gt;Github&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;b&gt;To avoid further costs, after the completion of this tutorial, please run:&lt;/b&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;b&gt;&lt;code&gt;terraform destroy&lt;/code&gt; in your terraform folder, to destroy your ec2 instance and security group&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;&lt;b&gt;&lt;code&gt;sls remove&lt;/code&gt; in your lambda folder, to remove all of your deployed resources related to the serverless framework.&lt;/b&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;div class="ltag__user ltag__user__id__779693"&gt;
    &lt;a href="/josevictorps" class="ltag__user__link profile-image-link"&gt;
      &lt;div class="ltag__user__pic"&gt;
        &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F779693%2F296b2115-6111-47c2-b904-72689f7c9de6.jpg" alt="josevictorps image"&gt;
      &lt;/div&gt;
    &lt;/a&gt;
  &lt;div class="ltag__user__content"&gt;
    &lt;h2&gt;
&lt;a class="ltag__user__link" href="/josevictorps"&gt;José Paiva&lt;/a&gt;Follow
&lt;/h2&gt;
    &lt;div class="ltag__user__summary"&gt;
      &lt;a class="ltag__user__link" href="/josevictorps"&gt;Backend Developer at Vaivoa School of Excellence and Cloud Architect &amp;amp; DevoOps Engineer at Carteira Global.&lt;/a&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2Fn8bndcx2jkn1jz1dy98v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fn8bndcx2jkn1jz1dy98v.png" alt="linha horizontal" width="800" height="3"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Disclaimer
&lt;/h1&gt;

&lt;p&gt;A VaiVoa incentiva seus Desenvolvedores em seu processo de crescimento e aceleração técnica. Os artigos publicados não traduzem a opinião da VaiVoa. A publicação obedece ao propósito de estimular o debate.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F1wmziqv74ghhgyi9p0om.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F1wmziqv74ghhgyi9p0om.png" alt="logo vaivoa" width="548" height="122"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>aws</category>
      <category>cloud</category>
      <category>terraform</category>
      <category>serverless</category>
    </item>
  </channel>
</rss>
