-
Notifications
You must be signed in to change notification settings - Fork 1
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
Introduce state file on s3 bucket #50
Open
mihai-satmarean
wants to merge
7
commits into
feature/aws-s3-module
Choose a base branch
from
mihai-satmarean-patch-1
base: feature/aws-s3-module
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0f91119
Create main.tf
mihai-satmarean 2c69490
Update main.tf
mihai-satmarean 0ed4092
Update terraform-apply.yml
mihai-satmarean 6bc14fd
Update terraform-apply.yml
mihai-satmarean 545ddc9
Update terraform-apply.yml
mihai-satmarean 2552eed
Update terraform-apply.yml
mihai-satmarean 978c3ac
Update terraform-apply.yml
mihai-satmarean File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,98 @@ | ||
name: Terraform apply | ||
name: Terraform Apply with Remote State | ||
run-name: ${{ github.actor }} is deploying on AWS 🚀 | ||
on: | ||
push: | ||
branches: | ||
- feature/aws-s3-module | ||
- mihai-satmarean-patch-1 | ||
jobs: | ||
Deploy-AWS-Ec2: | ||
runs-on: ubuntu-latest | ||
steps: | ||
# Step 1: Configure AWS credentials | ||
- name: Configure AWS credentials | ||
uses: aws-actions/configure-aws-credentials@v3 | ||
with: | ||
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID_MARIUS }} | ||
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY_MARIUS }} | ||
aws-region: eu-central-1 | ||
|
||
# Step 2: Checkout the repository code | ||
- name: Checkout Repository | ||
uses: actions/checkout@v4 | ||
- name: Deploy Infrastructure | ||
|
||
# Step 3: Ensure Terraform state S3 bucket exists | ||
- name: Create Terraform State Bucket | ||
run: | | ||
cd terraform/terraform-modules/tf-ec2-module/ | ||
cd terraform/terraform-modules/state-bucket | ||
terraform init | ||
terraform apply -auto-approve || echo "Bucket already exists, continuing..." | ||
# Step 4: Reconfigure Backend to Use S3 | ||
- name: Reconfigure Backend to S3 | ||
run: | | ||
cd terraform/terraform-modules/tf-ec2-module/ | ||
terraform init -backend-config="bucket=terraform_state_bucket" \ | ||
-backend-config="key=state/${GITHUB_REF#refs/heads/}/terraform.tfstate" \ | ||
-backend-config="region=eu-central-1" | ||
|
||
# Step 5: Apply Terraform with S3 backend | ||
- name: Finalize Infrastructure Deployment | ||
run: | | ||
cd terraform/terraform-modules/tf-ec2-module/ | ||
terraform apply -auto-approve | ||
|
||
# Step 6: Refresh Terraform State to ensure it's up to date with AWS | ||
- name: Refresh Terraform State | ||
run: | | ||
cd terraform/terraform-modules/tf-ec2-module/ | ||
terraform refresh | ||
# Step 7: Capture Terraform Outputs to Variables | ||
- name: Capture Terraform Outputs | ||
id: terraform_outputs | ||
run: | | ||
cd terraform/terraform-modules/tf-ec2-module/ | ||
export VPC_ID=$(terraform output -raw vpc_id) | ||
export PUBLIC_SUBNET_ID=$(terraform output -raw public_subnet_id) | ||
export PRIVATE_SUBNET_ID=$(terraform output -raw private_subnet_id) | ||
export SECURITY_GROUP_ID=$(terraform output -raw security_group_id) | ||
echo "VPC_ID=$VPC_ID" >> $GITHUB_ENV | ||
echo "PUBLIC_SUBNET_ID=$PUBLIC_SUBNET_ID" >> $GITHUB_ENV | ||
echo "PRIVATE_SUBNET_ID=$PRIVATE_SUBNET_ID" >> $GITHUB_ENV | ||
echo "SECURITY_GROUP_ID=$SECURITY_GROUP_ID" >> $GITHUB_ENV | ||
|
||
# Step 8: Validate Resources with AWS CLI | ||
- name: List Resources Created by Terraform | ||
run: | | ||
# List VPC | ||
echo "Listing VPC with ID: $VPC_ID" | ||
aws ec2 describe-vpcs --vpc-ids $VPC_ID || echo "Failed to list VPC with ID: $VPC_ID" | ||
|
||
# List Public Subnet | ||
echo "Listing Public Subnet with ID: $PUBLIC_SUBNET_ID" | ||
aws ec2 describe-subnets --subnet-ids $PUBLIC_SUBNET_ID || echo "Failed to list Public Subnet with ID: $PUBLIC_SUBNET_ID" | ||
|
||
# List Private Subnet | ||
echo "Listing Private Subnet with ID: $PRIVATE_SUBNET_ID" | ||
aws ec2 describe-subnets --subnet-ids $PRIVATE_SUBNET_ID || echo "Failed to list Private Subnet with ID: $PRIVATE_SUBNET_ID" | ||
|
||
# List Security Group | ||
echo "Listing Security Group with ID: $SECURITY_GROUP_ID" | ||
aws ec2 describe-security-groups --group-ids $SECURITY_GROUP_ID || echo "Failed to list Security Group with ID: $SECURITY_GROUP_ID" | ||
|
||
# Step 9: Destroy Infrastructure | ||
- name: Destroy Infrastructure | ||
run: | | ||
cd terraform/terraform-modules/tf-ec2-module/ | ||
terraform destroy -auto-approve | ||
|
||
# Step 10: Verify Resources are Destroyed | ||
- name: Verify Resources are Destroyed | ||
run: | | ||
# Verify that resources were destroyed by listing them again | ||
echo "Verifying VPC Destruction..." | ||
aws ec2 describe-vpcs --vpc-ids $VPC_ID || echo "VPC with ID $VPC_ID does not exist." | ||
echo "Verifying Public Subnet Destruction..." | ||
aws ec2 describe-subnets --subnet-ids $PUBLIC_SUBNET_ID || echo "Public Subnet with ID $PUBLIC_SUBNET_ID does not exist." | ||
echo "Verifying Private Subnet Destruction..." | ||
aws ec2 describe-subnets --subnet-ids $PRIVATE_SUBNET_ID || echo "Private Subnet with ID $PRIVATE_SUBNET_ID does not exist." | ||
echo "Verifying Security Group Destruction..." | ||
aws ec2 describe-security-groups --group-ids $SECURITY_GROUP_ID || echo "Security Group with ID $SECURITY_GROUP_ID does not exist." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# main.tf | ||
provider "aws" { | ||
region = "eu-central-1" | ||
} | ||
|
||
# Local backend for initial setup | ||
terraform { | ||
backend "local" { | ||
path = "terraform.tfstate" | ||
} | ||
} | ||
|
||
resource "aws_s3_bucket" "terraform_state_bucket" { | ||
bucket = "terraform-state-${terraform.workspace}" | ||
acl = "private" | ||
force_destroy = true # For simplicity in training scenarios; remove in production. | ||
|
||
tags = { | ||
Name = "Terraform State Bucket" | ||
} | ||
} | ||
|
||
resource "aws_s3_bucket_versioning" "state_bucket_versioning" { | ||
bucket = aws_s3_bucket.terraform_state_bucket.id | ||
|
||
versioning_configuration { | ||
status = "Enabled" | ||
} | ||
} | ||
|
||
# Terraform backend configuration post-initialization | ||
output "backend_config" { | ||
value = <<EOT | ||
bucket = "${aws_s3_bucket.terraform_state_bucket.bucket}" | ||
key = "state/${terraform.workspace}/terraform.tfstate" | ||
region = "${var.region}" | ||
EOT | ||
} | ||
|
||
variable "region" { | ||
default = "eu-central-1" | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aici trebuie sa gasim un mod sa mearga si pe alte branchuri decat hardcodat.
Inspiratie aici https://docs.github.com/en/actions/writing-workflows/workflow-syntax-for-github-actions#using-filters