Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Terraform #25

Open
wants to merge 23 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: CI-CD of ECR

on:
pull_request:
types: [closed] # Use brackets for proper syntax
branches: [main]

env:
AWS_REGION: us-east-1
ECR_REPOSITORY_APP: assignmentdocker-dev-ecr-repo
ECR_REPOSITORY_MYSQL: assignmentdocker-dev-mysql-ecr-repo

jobs:
build-and-push:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ env.AWS_REGION }}
aws-session-token: ${{ secrets.AWS_SESSION_TOKEN }} # Only include if using temporary credentials

- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1

- name: Build, tag, and push application image to Amazon ECR
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
IMAGE_TAG: ${{ github.sha }}
run: |
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY_APP:$IMAGE_TAG .
docker push $ECR_REGISTRY/$ECR_REPOSITORY_APP:$IMAGE_TAG

- name: Build, tag, and push MySQL image to Amazon ECR
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
IMAGE_TAG: ${{ github.sha }}
run: |
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY_MYSQL:$IMAGE_TAG -f Dockerfile.mysql .
docker push $ECR_REGISTRY/$ECR_REPOSITORY_MYSQL:$IMAGE_TAG
File renamed without changes.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Install the required MySQL package

# checked
sudo apt-get update -y
sudo apt-get install mysql-client -y

Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
flask==2.0.3
pymysql==1.0.2
cryptography==38.0.1
werkzeug==2.0.3
12 changes: 12 additions & 0 deletions terraform/dev/instances/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Ignore Terraform state and backup files
.terraform

# Ignore Terraform cache and provider files
.terraform/

# Optionally ignore the lock file
.terraform.lock.hcl

# Exclude SSH keys (if sensitive)
assignmentdocker-dev
assignmentdocker-dev.pub
Empty file.
156 changes: 156 additions & 0 deletions terraform/dev/instances/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
#----------------------------------------------------------
# ACS730 - Week 3 - Terraform Introduction
#
# Build EC2 Instances and Create ECR Repository
#
#----------------------------------------------------------

#this is for video purpose
# Define the provider
provider "aws" {
region = "us-east-1"
}

# Data source for AMI id
data "aws_ami" "latest_amazon_linux" {
owners = ["amazon"]
most_recent = true
filter {
name = "name"
values = ["amzn2-ami-hvm-*-x86_64-gp2"]
}
}

# Data source for availability zones in us-east-1
data "aws_availability_zones" "available" {
state = "available"
}

# Data block to retrieve the default VPC id
data "aws_vpc" "default" {
default = true
}

# Define tags locally
locals {
default_tags = merge(module.globalvars.default_tags, { "env" = var.env })
prefix = module.globalvars.prefix
name_prefix = "${local.prefix}-${var.env}"
}

# Retrieve global variables from the Terraform module
module "globalvars" {
source = "../../modules/globalvars"
}

# Create two more instances
resource "aws_instance" "my_instance_1" {
ami = data.aws_ami.latest_amazon_linux.id
instance_type = lookup(var.instance_type, var.env)
key_name = aws_key_pair.my_key.key_name
vpc_security_group_ids = [aws_security_group.my_sg.id]
associate_public_ip_address = false

lifecycle {
create_before_destroy = true
}

tags = merge(local.default_tags,
{
"Name" = "${local.name_prefix}-my-instance-1"
}
)
user_data = <<-EOF
#!/bin/bash
sudo yum update -y
sudo amazon-linux-extras install docker -y
sudo service docker start
sudo usermod -a -G docker ec2-user
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
EOF
}


# Adding SSH key to Amazon EC2
resource "aws_key_pair" "my_key" {
key_name = local.name_prefix
public_key = file("${local.name_prefix}.pub")
}

# Security Group
resource "aws_security_group" "my_sg" {
name = "allow_ssh"
description = "Allow SSH inbound traffic"
vpc_id = data.aws_vpc.default.id

ingress {
description = "SSH from everywhere"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
ipv6_cidr_blocks = ["::/0"]
}

ingress {
description = "Http from everywhere"
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
ipv6_cidr_blocks = ["::/0"]
}
ingress {
description = "Custom TCP"
from_port = 8081
to_port = 8083
protocol = "TCP"
cidr_blocks = ["0.0.0.0/0"]
}


egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
ipv6_cidr_blocks = ["::/0"]
}

tags = merge(local.default_tags,
{
"Name" = "${local.name_prefix}-sg"
}
)
}

resource "aws_eip" "static_eip_my_instance_1" {
instance = aws_instance.my_instance_1.id
tags = merge(local.default_tags,
{
"Name" = "${local.name_prefix}-eip"
}
)
}

# Elastic Container Registry (ECR) - Create a new repository
resource "aws_ecr_repository" "app_ecr_repo" {
name = lower("${local.name_prefix}-ecr-repo")
image_tag_mutability = "MUTABLE"
tags = merge(local.default_tags,
{
"Name" = "${local.name_prefix}-ecr-repo"
}
)
}
resource "aws_ecr_repository" "mysql_ecr_repo" {
name = lower("${local.name_prefix}-mysql-ecr-repo")
image_tag_mutability = "MUTABLE"

tags = merge(local.default_tags,
{
"Name" = "${local.name_prefix}-mysql-ecr-repo"
}
)
}
12 changes: 12 additions & 0 deletions terraform/dev/instances/output.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Step 10 - Add output variables
output "eip_my_instance" {
value = aws_eip.static_eip_my_instance.public_ip
}

output "eip_my_instance1" {
value = aws_eip.static_eip_my_instance_1.public_ip
}

output "eip_my_instance2" {
value = aws_eip.static_eip_my_instance_2.public_ip
}
Loading