@@ -14,10 +14,92 @@ provider "aws" {
1414}
1515
1616# KMS Key for EC2 encryption
17+ # resource "aws_kms_key" "ec2_key" {
18+ # description = "KMS key for EC2 encryption"
19+ # deletion_window_in_days = 30
20+ # enable_key_rotation = true
21+
22+ # tags = {
23+ # Name = "ec2-encryption-key"
24+ # Environment = var.environment
25+ # }
26+ # }
27+
28+ # Add this data source at the top of your file
29+ data "aws_caller_identity" "current" {}
30+
31+ # Replace your existing EC2 KMS key resource with this enhanced version
1732resource "aws_kms_key" "ec2_key" {
1833 description = " KMS key for EC2 encryption"
1934 deletion_window_in_days = 30
2035 enable_key_rotation = true
36+ is_enabled = true # Explicitly enable the key
37+
38+ policy = jsonencode ({
39+ Version = " 2012-10-17" ,
40+ Statement = [
41+ {
42+ Sid = " Enable IAM User Permissions" ,
43+ Effect = " Allow" ,
44+ Principal = {
45+ AWS = " arn:aws:iam::${ data . aws_caller_identity . current . account_id } :root"
46+ },
47+ Action = " kms:*" ,
48+ Resource = " *"
49+ },
50+ {
51+ Sid = " Allow EC2 service to use the key" ,
52+ Effect = " Allow" ,
53+ Principal = {
54+ Service = " ec2.amazonaws.com"
55+ },
56+ Action = [
57+ " kms:Encrypt" ,
58+ " kms:Decrypt" ,
59+ " kms:ReEncrypt*" ,
60+ " kms:GenerateDataKey*" ,
61+ " kms:DescribeKey"
62+ ],
63+ Resource = " *"
64+ },
65+ {
66+ Sid = " Allow autoscaling to use the key" ,
67+ Effect = " Allow" ,
68+ Principal = {
69+ AWS = " arn:aws:iam::${ data . aws_caller_identity . current . account_id } :role/aws-service-role/autoscaling.amazonaws.com/AWSServiceRoleForAutoScaling"
70+ },
71+ Action = [
72+ " kms:CreateGrant" ,
73+ " kms:ListGrants" ,
74+ " kms:RevokeGrant" ,
75+ " kms:Encrypt" ,
76+ " kms:Decrypt" ,
77+ " kms:ReEncrypt*" ,
78+ " kms:GenerateDataKey*" ,
79+ " kms:DescribeKey"
80+ ],
81+ Resource = " *"
82+ },
83+ {
84+ Sid = " Allow attachment of persistent resources" ,
85+ Effect = " Allow" ,
86+ Principal = {
87+ AWS = " *"
88+ },
89+ Action = [
90+ " kms:CreateGrant" ,
91+ " kms:ListGrants" ,
92+ " kms:RevokeGrant"
93+ ],
94+ Resource = " *" ,
95+ Condition = {
96+ Bool = {
97+ " kms:GrantIsForAWSResource" : " true"
98+ }
99+ }
100+ }
101+ ]
102+ })
21103
22104 tags = {
23105 Name = " ec2-encryption-key"
@@ -641,8 +723,8 @@ resource "aws_lb_listener" "app_listener" {
641723
642724# Launch Template for Auto Scaling Group with KMS encryption
643725resource "aws_launch_template" "app_launch_template" {
644- name = " csye6225_asg"
645- image_id = " ami-0ae4d60b84a0b35ba "
726+ name = " app-launch-template " # Changed from " csye6225_asg" for consistency
727+ image_id = var . ami_id
646728 instance_type = var. instance_type
647729 key_name = var. key_name
648730
@@ -669,16 +751,14 @@ resource "aws_launch_template" "app_launch_template" {
669751
670752 user_data = base64encode (<<- EOF
671753#!/bin/bash
672- # Retrieve DB password from Secrets Manager
673- DB_PASSWORD=$(aws secretsmanager get-secret-value --secret-id ${ aws_secretsmanager_secret . db_password_secret . name } --region ${ var . aws_region } --query SecretString --output text)
674754
675755# Create environment file for application
676756cat > /etc/environment <<EOL
677757DB_HOST=${ aws_db_instance . csye6225_db . address }
678758DB_PORT=${ var . db_port }
679759DB_NAME=${ aws_db_instance . csye6225_db . db_name }
680760DB_USER=${ aws_db_instance . csye6225_db . username }
681- DB_PASSWORD=$DB_PASSWORD
761+ DB_PASSWORD=${ aws_db_instance . csye6225_db . password }
682762S3_BUCKET=${ aws_s3_bucket . app_bucket . bucket }
683763AWS_REGION=${ var . aws_region }
684764EOL
@@ -694,29 +774,25 @@ mkdir -p /var/log/webapp
694774chmod 755 /var/log/webapp
695775chown saurabh_user:saurabh_group /var/log/webapp
696776
697- # Start CloudWatch agent
698- echo "Starting CloudWatch agent..."
699- systemctl enable amazon-cloudwatch-agent
700- systemctl restart amazon-cloudwatch-agent
777+ # # Start CloudWatch agent
778+ # echo "Starting CloudWatch agent..."
779+ # systemctl enable amazon-cloudwatch-agent
780+ # systemctl restart amazon-cloudwatch-agent
701781
702- # Ensure the webapp service starts automatically
703- systemctl enable webapp
704- systemctl restart webapp
782+ # # Ensure the webapp service starts automatically
783+ # systemctl enable webapp
784+ # systemctl restart webapp
705785
706- # Print status for troubleshooting purposes
707- echo "CloudWatch agent status:"
708- systemctl status amazon-cloudwatch-agent --no-pager
786+ # # Print status for troubleshooting purposes
787+ # echo "CloudWatch agent status:"
788+ # systemctl status amazon-cloudwatch-agent --no-pager
709789
710- echo "Webapp service status:"
711- systemctl status webapp --no-pager
790+ # echo "Webapp service status:"
791+ # systemctl status webapp --no-pager
712792
793+ # sudo systemctl restart webapp.service
713794
714- sudo systemctl restart webapp.service
715-
716- sudo systemctl restart webapp.service
717-
718-
719- echo "EC2 user data script completed"
795+ # echo "EC2 user data script completed"
720796EOF
721797 )
722798
@@ -732,11 +808,13 @@ EOF
732808 Name = " app-launch-template"
733809 Environment = var.environment
734810 }
811+
812+ depends_on = [aws_kms_key . ec2_key , aws_db_instance . csye6225_db , aws_s3_bucket . app_bucket ]
735813}
736814
737815# Auto Scaling Group
738816resource "aws_autoscaling_group" "app_asg" {
739- name = " app-auto-scaling-group-new"
817+ name = " app-auto-scaling-group" # Removed " -new" suffix
740818 min_size = 1
741819 max_size = 2
742820 desired_capacity = 2
@@ -762,6 +840,8 @@ resource "aws_autoscaling_group" "app_asg" {
762840 value = var. environment
763841 propagate_at_launch = true
764842 }
843+
844+ depends_on = [aws_launch_template . app_launch_template ]
765845}
766846
767847# Auto Scaling Policies
0 commit comments