diff --git a/.github/workflows/docker-image.yml b/.github/workflows/docker-image.yml new file mode 100644 index 0000000..5a67209 --- /dev/null +++ b/.github/workflows/docker-image.yml @@ -0,0 +1,44 @@ +name: Deploy to ECR phase1 + +on: + push: + branches: + - main + +jobs: + build: + name: Build Image + runs-on: ubuntu-latest + + steps: + - name: Check out code + uses: actions/checkout@v3 + + - name: Login to Amazon ECR + id: login-ecr + uses: aws-actions/amazon-ecr-login@v1 + env: + AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} + AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + AWS_SESSION_TOKEN: ${{ secrets.AWS_SESSION_TOKEN }} + AWS_REGION: us-east-1 + + - name: Build and Push Application + env: + ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} + ECR_REPOSITORY: webapp + IMAGE_TAG: latest + run: | + ls -ltra # List files for debugging + docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG -f Dockerfile . + docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG + + - name: Build and Push SQL + env: + ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} + ECR_REPOSITORY: mysql + IMAGE_TAG: latest + run: | + ls -ltra # List files for debugging + docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG -f Dockerfile_mysql . + docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG diff --git a/Dockerfile b/Dockerfile index 9dded45..c7b6bd2 100644 --- a/Dockerfile +++ b/Dockerfile @@ -10,4 +10,4 @@ RUN pip install --upgrade pip RUN pip install -r requirements.txt EXPOSE 8080 ENTRYPOINT [ "python3" ] -CMD [ "app.py" ] \ No newline at end of file +CMD [ "app.py" ] diff --git a/app.py b/app.py index 3ac7116..c7c12a8 100644 --- a/app.py +++ b/app.py @@ -101,7 +101,7 @@ def FetchData(): output["primary_skills"] = result[3] output["location"] = result[4] - except Exception as e: + except Exception as e: print(e) finally: @@ -133,4 +133,4 @@ def FetchData(): print("Color not supported. Received '" + COLOR + "' expected one of " + SUPPORTED_COLORS) exit(1) - app.run(host='0.0.0.0',port=8080,debug=True) + app.run(host='0.0.0.0',port=8080,debug=True) \ No newline at end of file diff --git a/mysql/Dockerfile b/mysql/Dockerfile new file mode 100644 index 0000000..8916ab4 --- /dev/null +++ b/mysql/Dockerfile @@ -0,0 +1,6 @@ +# mysql/Dockerfile +FROM mysql:5.7 +ENV MYSQL_ROOT_PASSWORD=root +ENV MYSQL_DATABASE=mydb +COPY schema.sql /docker-entrypoint-initdb.d/ + diff --git a/mysql/schema.sql b/mysql/schema.sql new file mode 100644 index 0000000..77108b1 --- /dev/null +++ b/mysql/schema.sql @@ -0,0 +1,3 @@ +-- mysql/schema.sql +CREATE TABLE users (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255)); + diff --git a/terraform_code/dev/instances/.terraform.lock.hcl b/terraform_code/dev/instances/.terraform.lock.hcl new file mode 100644 index 0000000..43c972a --- /dev/null +++ b/terraform_code/dev/instances/.terraform.lock.hcl @@ -0,0 +1,20 @@ +# This file is maintained automatically by "terraform init". +# Manual edits may be lost in future updates. + +provider "registry.terraform.io/hashicorp/aws" { + version = "3.74.0" + hashes = [ + "h1:YNOblHBUf+XTjGTfIIsAMGp4weXB+tmQrMPCrpmM1/U=", + "zh:00767509c13c0d1c7ad6af702c6942e6572aa6d529b40a00baacc0e73faafea2", + "zh:03aafdc903ad49c2eda03889f927f44212674c50e475a9c6298850381319eec2", + "zh:2de8a6a97b180f909d652f215125aa4683e99db15fcf3b28d62e3d542f875ed6", + "zh:3ac29ebc3af99028f4230a79f56606a0c2954b68767bd749b921a76eb4f3bd30", + "zh:50add2e2d118a15a644360eabc5a34cec59f2560b491f8fabf9c52ab83ca7b09", + "zh:85dd8e81910ab79f841a4a595fdd8ac358fbfe460956144afb0be3d81f91fe10", + "zh:895de83d0f0941fde31bfc53fa6b1ea276901f006bec221bbdee4771a04f3693", + "zh:a15c9724aac52d1ba5001d2d83e42843099b52b1638ea29d84e20be0f45fa4f1", + "zh:c982a64463bd73e9bff2589de214b1de0a571438d9015001f9eae45cfc3a2559", + "zh:e9ef973c18078324e43213ea1252c12b9441e566bf054ddfdbff5dd62f3035d9", + "zh:f297e705b0f339c8baa27ae70db5df9aa6578adfe1ea3d2ba8edc186512464eb", + ] +} diff --git a/terraform_code/dev/instances/config.tf b/terraform_code/dev/instances/config.tf new file mode 100644 index 0000000..e69de29 diff --git a/terraform_code/dev/instances/main.tf b/terraform_code/dev/instances/main.tf new file mode 100644 index 0000000..b1c1a8d --- /dev/null +++ b/terraform_code/dev/instances/main.tf @@ -0,0 +1,120 @@ + + +#---------------------------------------------------------- +# ACS730 - Week 3 - Terraform Introduction +# +# Build EC2 Instances +# +#---------------------------------------------------------- + +# 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" +} + +# Reference subnet provisioned by 01-Networking +resource "aws_instance" "my_amazon" { + 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}-Amazon-Linux" + } + ) +} + + +# 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"] + } + + 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" + } + ) +} + +# Elastic IP +resource "aws_eip" "static_eip" { + instance = aws_instance.my_amazon.id + tags = merge(local.default_tags, + { + "Name" = "${local.name_prefix}-eip" + } + ) +} +resource "aws_ecr_repository" "mysql" { + name = "mysql" +} + +# Create ECR repository for WebApp +resource "aws_ecr_repository" "webapp" { + name = "webapp" +} diff --git a/terraform_code/dev/instances/output.tf b/terraform_code/dev/instances/output.tf new file mode 100644 index 0000000..ad702fa --- /dev/null +++ b/terraform_code/dev/instances/output.tf @@ -0,0 +1,4 @@ +# Step 10 - Add output variables +output "eip" { + value = aws_eip.static_eip.public_ip +} \ No newline at end of file diff --git a/terraform_code/dev/instances/variables.tf b/terraform_code/dev/instances/variables.tf new file mode 100644 index 0000000..f25e07a --- /dev/null +++ b/terraform_code/dev/instances/variables.tf @@ -0,0 +1,22 @@ +# Instance type +variable "instance_type" { + default = { + "prod" = "t3.medium" + "test" = "t3.micro" + "staging" = "t2.micro" + "dev" = "t2.micro" + } + description = "Type of the instance" + type = map(string) +} + +# Variable to signal the current environment +variable "env" { + default = "dev" + type = string + description = "Deployment Environment" +} + + + + diff --git a/terraform_code/modules/globalvars/output.tf b/terraform_code/modules/globalvars/output.tf new file mode 100644 index 0000000..c06af26 --- /dev/null +++ b/terraform_code/modules/globalvars/output.tf @@ -0,0 +1,13 @@ +# Default tags +output "default_tags" { + value = { + "Owner" = "Dockerintro" + "App" = "Web" + "Project" = "CLO835" + } +} + +# Prefix to identify resources +output "prefix" { + value = "week1" +} \ No newline at end of file