Coder Social home page Coder Social logo

sltc-lab-2's Introduction

Deploying a Hello World API using Terraform and Github Actions(CI/CD)

This guide outlines the steps to create an S3 bucket for storing Terraform state files using the AWS CLI. It also covers optional steps for setting up state locking with DynamoDB.

Prerequisites

  • AWS CLI installed and configured with necessary permissions.
  • An AWS account with permissions to create S3 buckets and DynamoDB tables (if using state locking).

Installation and Configuration

Step 1: Configure AWS CLI

Ensure AWS CLI is installed and configured with your credentials:

aws configure

Step 2: Create S3 Bucket to store the Terraform State file

S3 bucket name should be globally unique.Make sure to use a unique bucket name.

aws s3 mb s3://chathra-tfstate-bkt --region us-east-1

Step 3: Enable Versioning

Enable versioning on your S3 bucket to keep the history of your state files:

aws s3api put-bucket-versioning --bucket chathra-tfstate-bkt --versioning-configuration Status=Enabled

Step 4: Set Up DynamoDB for State Locking (Optional)

If you want to use state locking, create a DynamoDB table:

aws dynamodb create-table \
    --table-name my-lock-table \
    --attribute-definitions AttributeName=LockID,AttributeType=S \
    --key-schema AttributeName=LockID,KeyType=HASH \
    --billing-mode PAY_PER_REQUEST

Step 5: Configure Terraform Backend

Add the following backend configuration in providers.tf

terraform {
  backend "s3" {
    bucket         = "my-terraform-state-bucket" # use the same bucket name used in step 2
    key            = "terraform.tfstate" 
    region         = "us-east-1"
    dynamodb_table = "my-lock-table" # optional (Only if you created the DynamoDB table in step 4) 
  }
}

Step 6: Create a new Github repository and set following secrets in development enviroment

This step will ensure github actions can authenticate and deploy the resources in AWS using Terraform.Your IAM user should have sufficient permissions in AWS. The AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY should be the security credentials of your AWS account user.

Alt text

Step 7: Once you prepare the code locally and commited to the local git repository, push it to the new repository that you created in Step 6, so it should trigger Github actions(CI/CD pipeline)

git remote set-url origin  <your new remote repo url(step 6)> 
git push origin main

Step 8: Access the deployed API

Alt text

Alt text

sltc-lab-2's People

Contributors

chathra222 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.