The Talent500 Blog
AWS

A Practical Guide to AWS Compute, Serverless, Terraform, and GitLab Integration- Part 1

This part-1 guide serves as the foundation for understanding the ecosystem of cloud computing, focusing on AWS’s compute and serverless offerings, the role of Terraform in infrastructure management, and GitLab’s capabilities in continuous integration and continuous deployment (CI/CD). In this part we’ll explore how these tools and technologies come together to create a robust, scalable, and efficient cloud infrastructure.

Overview of AWS Compute Services

Amazon EC2 (Elastic Compute Cloud): 
A foundational piece of AWS’s cloud computing platform, EC2 provides resizable compute capacity in the cloud, allowing users to run and manage servers across a global network of AWS data centres.

– Amazon ECS (Elastic Container Service): 
A highly scalable, high-performance container management service that supports Docker containers and allows you to run applications on a managed cluster of Amazon EC2 instances.

– AWS Lambda: 
A serverless compute service that lets you run code without provisioning or managing servers, automatically scaling your application by running code in response to events.

Introduction to Serverless Architecture

Serverless architecture allows developers to build and run applications and services without managing infrastructure. The cloud provider automatically provisions, scales, and manages the infrastructure required to run the code.

Key benefits include:

– Cost-efficiency: 
Pay only for the compute time you consume.

Scalability:
Automatically scales your application up or down based on demand.

Productivity: 
Focus on code rather than managing servers.

Terraform: The Infrastructure as Code (IaC) Solution

Terraform by HashiCorp allows you to define and provision cloud infrastructure using a high-level configuration language. It supports multiple cloud service providers, including AWS.

Key features:

– Infrastructure as Code: 
Manage infrastructure through configuration files that describe the to-be state of your resources.

– Execution Plans: 
Terraform generates an execution plan, showing what it will do before making any changes to your infrastructure.

Resource Graph: 
Understand how all your resources interrelate, ensuring non-destructive changes to your infrastructure.

GitLab: From Source to CI/CD

GitLab is a complete DevOps platform, delivered as a single application. It offers a wide range of tools from project planning and source code management to CI/CD, monitoring, and security. For AWS deployments, GitLab CI/CD can automate the process of testing and deploying your applications to AWS, integrating seamlessly with Terraform for infrastructure management.

How These Tools Work Together
The combination of AWS, Terraform, and GitLab provides a comprehensive ecosystem for deploying and managing cloud infrastructure:

– AWS offers the compute and serverless services needed to run applications.

– Terraform allows you to define and provision the AWS infrastructure in a repeatable and predictable manner, using code.

– GitLab automates the deployment process, integrating code changes with the infrastructure changes defined in Terraform.

This integrated approach enables teams to deploy faster, with more reliability and greater efficiency, all while maintaining the highest standards of security and compliance.

Setting Up Your Environment

Before diving into the practical aspects of AWS, Terraform, and GitLab CI/CD, it’s crucial to set up your development environment properly. This section walks you through the initial setup steps, ensuring that all the necessary tools and configurations are in place to start building your cloud infrastructure efficiently.

Preparing Your AWS Account

Sign Up or Log In: If you don’t already have an AWS account, sign up at aws.amazon.com

If you have an account, ensure you can log in successfully.

 Create an IAM User: 
For enhanced security, create an IAM (Identity and Access Management) user with administrative permissions. Use this user for all operations instead of the root account.
Secure Your Account: Activate MFA (Multi-Factor Authentication) on your account for added security.

Billing Alert: Set up a billing alert to monitor your spending and avoid unexpected charges.

Installing Terraform

Download Terraform: Go to the Terraform website and download the appropriate version for your operating system.
– Install TerraformFollow the installation instructions on the Terraform website to install Terraform on your local machine.
Verify Installation: Open a terminal or command prompt and type `terraform version` to ensure Terraform is correctly installed.

Setting Up GitLab (with GitLab CI/CD)

Sign Up for GitLab: 
If you don’t have a GitLab account, sign up at gitlab.com. If you already have an account, ensure you can log in.

Create a New Project: 
Once logged in, create a new project where you’ll store your Terraform configurations and application code.

Configure CI/CD: Explore the CI/CD settings in your project. You’ll be integrating these with Terraform and AWS in later chapters.

Configuring AWS CLI and IAM Roles for Secure Access

Install AWS CLI: Download and install the AWS Command Line Interface (CLI) from the AWS website. The CLI allows you to manage your AWS services from a terminal session.

– Configure AWS CLI: Run `aws configure` in your terminal. Enter your IAM user credentials (access key ID and secret access key) and default region.

Create IAM Roles: Understand the concept of IAM Roles for AWS services and how they offer a secure way to grant permissions to entities you trust. You’ll use IAM roles to grant permissions to Terraform and GitLab CI/CD to access and manage AWS resources.

Preparatory Steps for Seamless Development

– Organize Your Workspace: Create a dedicated directory on your machine for your projects. It’s a good practice to keep your Terraform configurations separate from application code initially.

Understand the AWS Management Console: Familiarize yourself with the AWS Management Console, as it’s a helpful tool for understanding the resources you’ll be managing with Terraform.

Familiarize Yourself with GitLab Repositories: Get comfortable with pushing and pulling code from your GitLab repository, as it will be crucial for CI/CD integration.

By completing this section, you have laid the groundwork for a smooth development experience. You now have access to the core tools and services you’ll be using throughout this guide. With your environment set up, you’re ready to dive into the specifics of using Terraform with AWS, starting with learning the basics of Terraform. 

Terraform Basics for AWS: Terraform is a powerful tool that allows you to create, change, and improve infrastructure reliably and predictably. This section introduces the basics of Terraform, focusing on its application within AWS to manage infrastructure as code. In this section you’ll learn Terraform’s syntax, how to manage AWS resources, and the key concepts of state management and modules.

Terraform Syntax and File Structure

HCL (HashiCorp Configuration Language): Terraform uses HCL, a human-readable language for describing your infrastructure. Learn the basic syntax, including how to declare resources, variables, and outputs.
– File Structure: Understand the recommended file layout for a Terraform project. A typical setup includes main.tf (for resources), variables.tf (for variable declarations), and outputs.tf (for outputting resource information).

Managing AWS Resources with Terraform

Provider Configuration: Learn how to configure the AWS provider in Terraform to manage AWS resources. This includes specifying your AWS access keys, region, and other provider-specific settings.

provider “aws” {

  region     = “us-west-2”

  access_key = “your-access-key”

  secret_key = “your-secret-key”

}

 Resource Declaration: Dive into declaring AWS resources, such as EC2 instances or S3 buckets, using Terraform. Understand the importance of resource attributes and how to reference them.

resource “aws_instance” “example” {
  ami           = “ami-0c55b159cbfafe1f0”
  instance_type = “t2.micro”
}

 Applying Changes: Learn how to apply your Terraform configurations to create or update your AWS infrastructure. Understand the `terraform init`, `terraform plan`, and `terraform apply` commands and their roles in the lifecycle of a Terraform project.

Terraform State Management

  • Understanding State: Terraform maintains a state file (`terraform.tfstate`) to keep track of your infrastructure and configuration. Grasp why state is crucial for Terraform to function correctly and how to manage state files securely.
  • Remote State: Explore the concept of remote state storage, which allows team members to work collaboratively on Terraform configurations. Learn how to configure Terraform to store state in a remote backend like AWS S3.

Terraform Modules: Reusability and Organization

– Introduction to Modules: Discover how to use Terraform modules to organize and reuse your code. Modules allow you to group resources together and reuse this group in different parts of your infrastructure or across different projects.
Creating and Using ModulesLearn how to create your own Terraform modules and how to use modules provided by the Terraform community.

module “ec2_instance” {
  source = “./modules/ec2”
  instance_type = “t2.micro”
  ami = “ami-0c55b159cbfafe1f0”
}

 Module Sources: Understand the different sources from which you can load modules, including local paths, GitHub repos, and the Terraform Registry.

Key Takeaways

By the end of this section, you should be comfortable with the basics of using Terraform to manage AWS resources. You’ve learned about Terraform’s syntax, how to configure the AWS provider, declare resources, manage Terraform state, and utilize modules for efficient infrastructure management. With this foundation, you’re ready to start deploying AWS compute resources using Terraform, which I’ll cover in the next section.

Deploying AWS Compute Resources with Terraform

This section dives into the practical aspects of using Terraform to deploy AWS compute resources. You’ll learn how to create and manage EC2 instances, set up ECS for container management, and configure load balancers and auto-scaling to ensure your applications are highly available and scalable.

Deploying an EC2 Instance

The Amazon EC2 service provides resizable compute capacity in the cloud, making it easier to develop and scale applications. Here’s how to deploy an EC2 instance with Terraform:

Define an EC2 Instance in Terraform:

resource “aws_instance” “my_ec2” {
  ami           = “ami-0c55b159cbfafe1f0” # Example AMI ID
  instance_type = “t2.micro”                    #type of instance
  key_name      = “my-key-pair”               #instance key pair
  security_groups = [“my-security-group”] # instance security group

  tags = {
    Name = “MyEC2Instance”                       #tag for the instance
  }
}

Apply ConfigurationUse `terraform apply` to launch your instance. Terraform communicates with AWS to create the resources defined in your configuration files.

– Accessing the Instance: Once the instance is up, you can access it using SSH with the key pair specified during setup.

Setting Up ECS (Elastic Container Service)

AWS ECS allows you to run containerized applications on AWS. To deploy an ECS cluster with Terraform:

Define the ECS Cluster:

resource “aws_ecs_cluster” “my_cluster” {
  name = “my-ecs-cluster”
}

 Define the ECS Task Definition and Service: 
Task definitions specify how containers should be deployed, and services maintain the desired number of instances of the task definition.

resource “aws_ecs_task_definition” “my_task” {
  family                = “my-task-family”
  network_mode          = “awsvpc”
  requires_compatibilities = [“FARGATE”]
  cpu                   = “256”
  memory                = “512”
  execution_role_arn    = aws_iam_role.ecs_task_execution_role.arn
  container_definitions = jsonencode([
    {
      name      = “my-container”,
      image     = “my-container-image”,
      cpu       = 256,
      memory    = 512,
      essential = true,
    }
  ])
}

resource “aws_ecs_service” “my_service” {
  name            = “my-service”
  cluster         = aws_ecs_cluster.my_cluster.id
  task_definition = aws_ecs_task_definition.my_task.arn
  desired_count   = 2
  launch_type     = “FARGATE”
}

Managing Load Balancers and Auto Scaling Groups

To ensure your application is scalable and highly available, you can use load balancers and auto-scaling groups.

Load Balancers:
 Distribute incoming application traffic across multiple targets, such as EC2 instances.

resource “aws_lb” “my_lb” {
  name               = “my-load-balancer”
  internal           = false
  load_balancer_type = “application”
  security_groups    = [aws_security_group.lb_sg.id]
  subnets            = aws_subnet.public.*.id
}
resource “aws_lb_target_group” “my_tg” {
  name     = “my-target-group”
  port     = 80
  protocol = “HTTP”
  vpc_id   = aws_vpc.main.id
}

resource “aws_lb_listener” “my_listener” {
  load_balancer_arn = aws_lb.my_lb.arn
  port              = 80
  protocol          = “HTTP”
  default_action {
    type             = “forward”
    target_group_arn = aws_lb_target_group.my_tg.arn
  }
}

Auto Scaling Groups: Automatically adjust the number of EC2 instances to meet the demand.
resource “aws_autoscaling_group” “my_asg” {
  launch_configuration = aws_launch_configuration.my_lc.name
  min_size             = 1
  max_size             = 10
  desired_capacity     = 2
  vpc_zone_identifier  = aws_subnet.public.*.id

  tag {
    key                 = “Name”
    value               = “my-autoscale-instance”
    propagate_at_launch = true
  }
}

Embracing Serverless with AWS Lambda and Terraform

AWS Lambda is a serverless compute service that lets you run code without provisioning or managing servers, enabling you to build applications that automatically scale with high availability. This section guides you through deploying AWS Lambda functions using Terraform, showcasing how serverless architectures can be efficiently managed as part of your infrastructure as code (IaC) practices.

Introduction to AWS Lambda

AWS Lambda allows you to run code in response to triggers such as changes in data, shifts in system state, or actions by users. Lambda can automatically manage the compute resources for you, making it easy to build applications that respond quickly to new information.

Key Features:
Automatic ScalingLambda automatically scales your application by running code in response to each trigger.
Subsecond Metering: With Lambda, you’re charged for every 100 milliseconds your code executes and the number of times your code is triggered.
Integrated Security Model: Use AWS Identity and Access Management (IAM) to set permissions and securely access other AWS services.
Deploying a Lambda Function with Terraform

To deploy a Lambda function with Terraform, you need to:

– Define the Lambda Function:
 Specify the AWS Lambda resource in Terraform, including the function’s code, handler, runtime, and role that it assumes when it executes.

resource “aws_lambda_function” “my_lambda” {
  function_name = “MyLambdaFunction”

  s3_bucket = “my_lambda_functions”
  s3_key    = “lambda_function_payload.zip”

  handler = “index.handler”
  runtime = “nodejs14.x”

  role = aws_iam_role.lambda_exec_role.arn
}


Create an IAM Role for Lambda Execution:
 Lambda functions require an IAM role with permission to execute the Lambda function and access AWS services.

resource “aws_iam_role” “lambda_exec_role” {
  name = “lambda_exec_role”

  assume_role_policy = jsonencode({
    Version = “2012-10-17”
    Statement = [
      {
        Action = “sts:AssumeRole”
        Principal = {
          Service = “lambda.amazonaws.com”
        }
        Effect = “Allow”
        Sid = “”
      },
    ]
  })
}

 Deploy the Function: 
Run `terraform apply` to deploy your Lambda function. Once deployed, the function can be invoked manually, or you can set up triggers from other AWS services.

Integrating API Gateway for Serverless Applications

AWS API Gateway can act as a front door for applications to access data, business logic, or functionality from your Lambda functions. Integrating API Gateway with Lambda allows you to create RESTful APIs and WebSocket APIs that are scalable and secure.

Define the API Gateway Resource: 
Create an API Gateway resource and integrate it with your Lambda function using Terraform.
resource “aws_api_gateway_rest_api” “MyDemoAPI” {
  name        = “MyDemoAPI”
  description = “This is my API for demonstration purposes”
}
resource “aws_api_gateway_resource” “MyDemoResource” {
  rest_api_id = aws_api_gateway_rest_api.MyDemoAPI.id
  parent_id   = aws_api_gateway_rest_api.MyDemoAPI.root_resource_id
  path_part   = “mydemoresource”
}
resource “aws_api_gateway_method” “MyDemoMethod” {
  rest_api_id   = aws_api_gateway_rest_api.MyDemoAPI.id
  resource_id   = aws_api_gateway_resource.MyDemoResource.id
  http_method   = “GET”
  authorization = “NONE”
}
resource “aws_api_gateway_integration” “MyDemoIntegration” {
  rest_api_id = aws_api_gateway_rest_api.MyDemoAPI.id
  resource_id = aws_api_gateway_resource.MyDemoResource.id
  http_method = aws_api_gateway_method.MyDemoMethod.http_method
   integration_http_method = “POST”
  type                    = “AWS_PROXY”
  uri                     = aws_lambda_function.my_lambda.invoke_arn

}
 Deploy and Test
After deploying your API Gateway resource, you can test the integration by invoking the API endpoint. The API Gateway will forward the request to your Lambda function and return the response.

Conclusion

This section covered the essentials of deploying AWS Lambda functions with Terraform and integrating them with API Gateway to build serverless applications. By embracing serverless technologies like Lambda, you can build scalable, efficient, and cost-effective applications. With Terraform, managing and deploying these serverless components becomes seamless, allowing you to incorporate serverless architectures into your broader infrastructure as code practices.

0
Avatar

Priyam Vaidya

A certified cloud architect (Azure and AWS) with over 15 years of experience in IT. Currently working as Sr Cloud Infrastructure Engineer. Love to explore and train others on new technology

Add comment