@@ -6,18 +6,64 @@ import {
66 requireAwsModules ,
77} from '@/generators/addons/aws/dependencies' ;
88import {
9+ INFRA_CORE_DATA_PATH ,
10+ INFRA_CORE_LOCALS_PATH ,
911 INFRA_CORE_MAIN_PATH ,
1012 INFRA_CORE_OUTPUTS_PATH ,
1113 INFRA_CORE_VARIABLES_PATH ,
14+ MODULES_LOCALS_INDICATOR ,
1215} from '@/generators/terraform/constants' ;
13- import { appendToFile , copy } from '@/helpers/file' ;
16+ import { appendToFile , copy , injectToFile } from '@/helpers/file' ;
1417
1518import {
1619 AWS_SECURITY_GROUP_MAIN_PATH ,
1720 AWS_SECURITY_GROUP_OUTPUTS_PATH ,
1821 AWS_TEMPLATE_PATH ,
1922} from '../constants' ;
2023
24+ const albLocalesContent = dedent `
25+ ###ALB Locals###
26+ alb_s3_bucket_policy = {
27+ Version = "2012-10-17"
28+ Statement = [
29+ {
30+ Effect = "Allow"
31+ Principal = {
32+ AWS = [
33+ "\${data.aws_elb_service_account.elb_service_account.arn}"
34+ ]
35+ }
36+ Action = "s3:PutObject"
37+ Resource = "arn:aws:s3:::\${module.s3_alb_access_log.aws_s3_bucket_name}/AWSLogs/*"
38+ },
39+ {
40+ Effect = "Allow",
41+ Principal = {
42+ Service = "delivery.logs.amazonaws.com"
43+ }
44+ Action = "s3:PutObject"
45+ Resource = "arn:aws:s3:::\${module.s3_alb_access_log.aws_s3_bucket_name}/AWSLogs/*",
46+ Condition = {
47+ StringEquals = {
48+ "s3:x-amz-acl" = "bucket-owner-full-control"
49+ }
50+ }
51+ },
52+ {
53+ Effect = "Allow",
54+ Principal = {
55+ Service = "delivery.logs.amazonaws.com"
56+ }
57+ Action = "s3:GetBucketAcl"
58+ Resource = "arn:aws:s3:::\${module.s3_alb_access_log.aws_s3_bucket_name}"
59+ }
60+ ]
61+ }` ;
62+
63+ const albDataContent = dedent `
64+ ###ALB Locals###
65+ data "aws_elb_service_account" "elb_service_account" {}` ;
66+
2167const albVariablesContent = dedent `
2268 variable "health_check_path" {
2369 description = "Application health check path"
@@ -30,15 +76,31 @@ const albVariablesContent = dedent`
3076 }` ;
3177
3278const albModuleContent = dedent `
79+ module "s3_alb_access_log" {
80+ source = "../modules/s3"
81+
82+ env_namespace = local.env_namespace
83+ bucket_name = "\${local.env_namespace}-alb-access-logs-\${data.aws_caller_identity.current.account_id}"
84+ force_destroy = true
85+ }
86+
3387 module "alb" {
3488 source = "../modules/alb"
3589
36- vpc_id = module.vpc.vpc_id
37- env_namespace = local.env_namespace
38- app_port = var.app_port
39- subnet_ids = module.vpc.public_subnet_ids
40- security_group_ids = module.security_group.alb_security_group_ids
41- health_check_path = var.health_check_path
90+ vpc_id = module.vpc.vpc_id
91+ env_namespace = local.env_namespace
92+ app_port = var.app_port
93+ subnet_ids = module.vpc.public_subnet_ids
94+ security_group_ids = module.security_group.alb_security_group_ids
95+ health_check_path = var.health_check_path
96+ bucket_access_log_name = module.s3_alb_access_log.aws_s3_bucket_name
97+ }
98+
99+ module "s3_bucket_access_log_policy" {
100+ source = "../modules/s3/bucket_policy"
101+
102+ s3_bucket_name = module.s3_alb_access_log.aws_s3_bucket_name
103+ s3_bucket_policy = local.alb_s3_bucket_policy
42104 }` ;
43105
44106const albOutputsContent = dedent `
@@ -104,6 +166,10 @@ const applyAwsAlb = async (options: AwsOptions) => {
104166 await requireAwsModules ( 'alb' , 'securityGroup' , options ) ;
105167
106168 copy ( `${ AWS_TEMPLATE_PATH } /modules/alb` , 'modules/alb' , options . projectName ) ;
169+ injectToFile ( INFRA_CORE_LOCALS_PATH , albLocalesContent , options . projectName , {
170+ insertAfter : MODULES_LOCALS_INDICATOR ,
171+ } ) ;
172+ appendToFile ( INFRA_CORE_DATA_PATH , albDataContent , options . projectName ) ;
107173 appendToFile ( INFRA_CORE_MAIN_PATH , albModuleContent , options . projectName ) ;
108174 appendToFile (
109175 INFRA_CORE_VARIABLES_PATH ,
0 commit comments