This repository contains Terraform code
- To launch an AWS EC2 instance for different stages
- To create an s3 bucket to store logs for different stages and
- To streams logs to cloudwatch
- Created the SNS topic and configure the alarm so that it can publish the SNS Message.
To trigger the cloudwatch alarm for SNS message:
- Put your email at alert_email variable in ./terraform/variables.tf file.
- SSH into EC2 and simulate a log entry with suitable permissions:
- echo "ERROR: Simulated failure" >> /home/ec2-user/techeazy-devops/application.log
- You will get SNS message at your email.
You can use this repo as
- ✅ Locally on your machine
- ✅ Automatically through a GitHub Actions workflow
Click on the fork button at the top right of this GitHub repo to create your own copy.
You need an AWS IAM user with programmatic access. Set your AWS credentials as repository secrets:
Navigate to:
Settings > Secrets and variables > Actions > New repository secret
Add the following secrets:
AWS_ACCESS_KEY_ID: Your AWS Access Key IDAWS_SECRET_ACCESS_KEY: Your AWS Secret Access KeyGH_TOKEN: Your Github Token
3. 🪣 Create an S3 Bucket to manage Terraform State file so that you can also delete the infrastructure using github actions
- Create a globally unique S3 bucket.
- Configure the S3 bucket name in the
bucketattribute inside thebackend.tffile located in the./terraform/directory and./terraform/shared/. - Commit the updated
backend.tf.
This ensures the Terraform state is stored remotely and allows GitHub Actions to manage infrastructure across runs.
To create AWS resources:
- Go to the Actions tab in your GitHub repo.
- Select the
Deploy.ymlworkflow. - Click "Run workflow".
This will automatically provision the EC2 instance and the S3 bucket and validate application hosting.
To delete the AWS resources created by Terraform:
- Go to the Actions tab.
- Select the
Destroy.ymlworkflow. - Click "Run workflow".
To delete the AWS resources created by Terraform:
- Go to the Actions tab.
- Select the
shared.ymlworkflow. - Click "Run workflow".
Before you begin, make sure you have the following installed:
Run:
aws configureYou’ll be prompted to enter:
- AWS Access Key ID
- AWS Secret Access Key
- Default region name (e.g.,
ap-south-1) - Default output format (optional)
git clone https://github.com/rajgupta2/tech_eazy_devops_rajgupta2.git
cd tech_eazy_devops_rajgupta2/terraform- Create a globally unique S3 bucket.
- Configure the S3 bucket name in the
bucketattribute inside thebackend.tffile located in the./terraform/directory and./terraform/shared/. - Commit the updated
backend.tf.
This ensures the Terraform state is stored remotely and allows to manage infrastructure across different stages.
terraform initterraform planterraform applyType yes when prompted.
terraform init -backend-config="key=<stage>/terraform.tfstate"
#stage should be configured at your own such as dev or prod at above cmd.terraform apply -var "stage=<stage>"
#The same stage should be configured as done in above step-6.You will be prompted to enter github token, Please enter github token.
Type yes when prompted.
- An EC2 instance will be provisioned.
- Java and Maven will be installed automatically on the instance.
- Logs will be stored in the configured S3 bucket according to stages.
- Application will be deployed successfully.
- You can also read log by connecting to
EC2-instances: InstanceWithS3ReadsAccessand running below command.aws s3 ls s3://logs-bucket-rajgupta2-ap-south-1 #This lists all top-level files/folders in the bucket. Make sure you change the bucket name according to your if you configure your bucket name. aws s3 cp s3://logs-bucket-rajgupta2-ap-south-1/app/logs/file_name.log . #You need to configure file name in above script.. cat file_name.log #to read the log
To avoid charges, destroy all resources created by Terraform:
terraform destroy -var "stage=<stage>" -auto-approve
#The same stage should be configured as done in above step-6.You will be prompted to enter github token, Please enter github token.
Feel free to fork this repo, open issues, or submit PRs to improve the setup.