Skip to content

Commit 0aa0817

Browse files
committed
[#323]Make s3 module to be generic, and move locals out of main file and create dedicated locals and data files
1 parent ab963e8 commit 0aa0817

File tree

15 files changed

+168
-117
lines changed

15 files changed

+168
-117
lines changed

src/commands/generate/index.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ describe('Generator command', () => {
6565
expect(postProcess).toHaveBeenCalledTimes(1);
6666
});
6767

68-
it('contains processed project name in main files', () => {
69-
const mainFiles = ['shared/main.tf', 'core/main.tf'];
70-
mainFiles.forEach((fileName) => {
68+
it('contains processed project name in locals files', () => {
69+
const localsFiles = ['shared/locals.tf', 'core/locals.tf'];
70+
localsFiles.forEach((fileName) => {
7171
expect(processedDirectoryName).toHaveContentInFile(
7272
fileName,
7373
`project_name = "${processedDirectoryName}"`,

src/generators/addons/aws/modules/alb.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ describe('ALB add-on', () => {
4646
'core/variables.tf',
4747
'modules/alb/main.tf',
4848
'modules/alb/variables.tf',
49+
'modules/alb/outputs.tf',
4950
];
5051

5152
expect(projectDir).toHaveFiles(expectedFiles);

src/generators/addons/aws/modules/alb.ts

Lines changed: 73 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,64 @@ import {
66
requireAwsModules,
77
} from '@/generators/addons/aws/dependencies';
88
import {
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

1518
import {
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+
2167
const albVariablesContent = dedent`
2268
variable "health_check_path" {
2369
description = "Application health check path"
@@ -30,15 +76,31 @@ const albVariablesContent = dedent`
3076
}`;
3177

3278
const 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

44106
const 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,

src/generators/addons/aws/modules/s3.test.ts

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { applyTerraformCore } from '@/generators/terraform';
33
import { remove } from '@/helpers/file';
44

55
import applyTerraformAwsProvider from './core/provider';
6-
import applyAwsS3, { s3ModuleContent, s3OutputsContent } from './s3';
6+
import applyAwsS3 from './s3';
77

88
jest.mock('inquirer', () => {
99
return {
@@ -41,20 +41,11 @@ describe('S3 add-on', () => {
4141
'modules/s3/main.tf',
4242
'modules/s3/variables.tf',
4343
'modules/s3/outputs.tf',
44+
'modules/s3/bucket_policy/main.tf',
45+
'modules/s3/bucket_policy/variables.tf',
4446
];
4547

4648
expect(projectDir).toHaveFiles(expectedFiles);
4749
});
48-
49-
it('adds S3 module to main.tf', () => {
50-
expect(projectDir).toHaveContentInFile('core/main.tf', s3ModuleContent);
51-
});
52-
53-
it('adds S3 outputs to outputs.tf', () => {
54-
expect(projectDir).toHaveContentInFile(
55-
'core/outputs.tf',
56-
s3OutputsContent
57-
);
58-
});
5950
});
6051
});
Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,20 @@
1-
import { dedent } from 'ts-dedent';
2-
31
import { AwsOptions } from '@/generators/addons/aws';
42
import { isAwsModuleAdded } from '@/generators/addons/aws/dependencies';
5-
import {
6-
INFRA_CORE_MAIN_PATH,
7-
INFRA_CORE_OUTPUTS_PATH,
8-
} from '@/generators/terraform/constants';
9-
import { appendToFile, copy } from '@/helpers/file';
3+
import { copy } from '@/helpers/file';
104

115
import { AWS_TEMPLATE_PATH } from '../constants';
126

13-
const s3OutputsContent = dedent`
14-
output "s3_alb_log_bucket_name" {
15-
description = "S3 bucket name for ALB log"
16-
value = module.s3.aws_alb_log_bucket_name
17-
}`;
18-
19-
const s3ModuleContent = dedent`
20-
module "s3" {
21-
source = "../modules/s3"
22-
23-
env_namespace = local.env_namespace
24-
}`;
25-
267
const applyAwsS3 = async (options: AwsOptions) => {
278
if (isAwsModuleAdded('s3', options.projectName)) {
289
return;
2910
}
3011

3112
copy(`${AWS_TEMPLATE_PATH}/modules/s3`, 'modules/s3', options.projectName);
32-
appendToFile(INFRA_CORE_OUTPUTS_PATH, s3OutputsContent, options.projectName);
33-
appendToFile(INFRA_CORE_MAIN_PATH, s3ModuleContent, options.projectName);
13+
copy(
14+
`${AWS_TEMPLATE_PATH}/modules/s3/bucket_policy`,
15+
'modules/s3/bucket_policy',
16+
options.projectName
17+
);
3418
};
3519

3620
export default applyAwsS3;
37-
export { s3ModuleContent, s3OutputsContent };

src/generators/terraform/constants.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,30 @@ const INFRA_CORE_PATH = 'core/';
22
const INFRA_CORE_MAIN_PATH = `${INFRA_CORE_PATH}/main.tf`;
33
const INFRA_CORE_OUTPUTS_PATH = `${INFRA_CORE_PATH}/outputs.tf`;
44
const INFRA_CORE_VARIABLES_PATH = `${INFRA_CORE_PATH}/variables.tf`;
5+
const INFRA_CORE_LOCALS_PATH = `${INFRA_CORE_PATH}/locals.tf`;
6+
const INFRA_CORE_DATA_PATH = `${INFRA_CORE_PATH}/data.tf`;
57

68
const INFRA_SHARED_PATH = 'shared';
79
const INFRA_SHARED_MAIN_PATH = `${INFRA_SHARED_PATH}/main.tf`;
810
const INFRA_SHARED_OUTPUTS_PATH = `${INFRA_SHARED_PATH}/outputs.tf`;
911
const INFRA_SHARED_VARIABLES_PATH = `${INFRA_SHARED_PATH}/variables.tf`;
12+
const INFRA_SHARED_LOCALS_PATH = `${INFRA_SHARED_PATH}/locals.tf`;
13+
const INFRA_SHARED_DATA_PATH = `${INFRA_SHARED_PATH}/data.tf`;
14+
15+
const MODULES_LOCALS_INDICATOR = `### Modules Locals ###`;
1016

1117
export {
1218
INFRA_CORE_PATH,
1319
INFRA_CORE_MAIN_PATH,
1420
INFRA_CORE_OUTPUTS_PATH,
1521
INFRA_CORE_VARIABLES_PATH,
22+
INFRA_CORE_LOCALS_PATH,
23+
INFRA_CORE_DATA_PATH,
1624
INFRA_SHARED_PATH,
1725
INFRA_SHARED_MAIN_PATH,
1826
INFRA_SHARED_OUTPUTS_PATH,
1927
INFRA_SHARED_VARIABLES_PATH,
28+
INFRA_SHARED_LOCALS_PATH,
29+
INFRA_SHARED_DATA_PATH,
30+
MODULES_LOCALS_INDICATOR,
2031
};

src/generators/terraform/index.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ import { dedent } from 'ts-dedent';
22

33
import { GeneralOptions } from '@/commands/generate';
44
import {
5-
INFRA_CORE_MAIN_PATH,
6-
INFRA_SHARED_MAIN_PATH,
5+
INFRA_CORE_LOCALS_PATH,
6+
INFRA_SHARED_LOCALS_PATH,
7+
INFRA_CORE_DATA_PATH,
8+
MODULES_LOCALS_INDICATOR,
79
} from '@/generators/terraform/constants';
810
import { copy, rename, appendToFile } from '@/helpers/file';
911

@@ -13,13 +15,20 @@ const applyTerraformCore = async (generalOptions: GeneralOptions) => {
1315
copy('terraform/', '.', projectName);
1416

1517
const coreLocalsContent = dedent`
16-
locals {
17-
project_name = "${projectName}"
18-
env_namespace = "\${local.project_name}-\${var.environment}"
19-
}`;
18+
locals {
19+
project_name = "${projectName}"
20+
env_namespace = "\${local.project_name}-\${var.environment}"
2021
21-
appendToFile(INFRA_CORE_MAIN_PATH, coreLocalsContent, projectName);
22-
appendToFile(INFRA_SHARED_MAIN_PATH, coreLocalsContent, projectName);
22+
${MODULES_LOCALS_INDICATOR}
23+
}`;
24+
25+
const coreDatContent = dedent`
26+
data "aws_caller_identity" "current" {}
27+
data "aws_partition" "current" {}`;
28+
29+
appendToFile(INFRA_CORE_LOCALS_PATH, coreLocalsContent, projectName);
30+
appendToFile(INFRA_SHARED_LOCALS_PATH, coreLocalsContent, projectName);
31+
appendToFile(INFRA_CORE_DATA_PATH, coreDatContent, projectName);
2332

2433
// Need to rename .gitignore to gitignore because NPN package doesn't include .gitignore
2534
// https://github.com/npm/npm/issues/3763
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
locals {
2+
enable_stickiness = false
3+
}

templates/addons/aws/modules/alb/main.tf

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
locals {
2-
enable_stickiness = false
3-
}
4-
51
# trivy:ignore:AVD-AWS-0053
62
resource "aws_lb" "main" {
73
name = "${var.env_namespace}-alb"
@@ -14,7 +10,7 @@ resource "aws_lb" "main" {
1410
drop_invalid_header_fields = true
1511

1612
access_logs {
17-
bucket = "${var.env_namespace}-alb-log"
13+
bucket = var.bucket_access_log_name
1814
enabled = true
1915
}
2016
}

templates/addons/aws/modules/alb/variables.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ variable "subnet_ids" {
88
type = list(string)
99
}
1010

11+
variable "bucket_access_log_name" {
12+
description = "The name of the S3 bucket for ALB access logs"
13+
type = string
14+
}
15+
1116
variable "security_group_ids" {
1217
description = "A list of security group IDs to assign to the LB"
1318
type = list(string)

0 commit comments

Comments
 (0)