Skip to content

Commit 6b20d9c

Browse files
committed
[#316] Make s3 module to be generic
1 parent ab963e8 commit 6b20d9c

File tree

17 files changed

+165
-99
lines changed

17 files changed

+165
-99
lines changed

src/generators/addons/aws/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
applyAwsRegion,
1111
applyAwsSecurityGroup,
1212
applyAwsVpc,
13+
applyTerraformAwsData,
1314
} from './modules';
1415

1516
const awsChoices = [
@@ -66,6 +67,7 @@ const generateAwsTemplate = async (
6667

6768
case 'advanced':
6869
await applyProviderAndRegion(awsOptions);
70+
await applyTerraformAwsData(awsOptions);
6971
await applyAwsVpc(awsOptions);
7072
await applyAwsSecurityGroup(awsOptions);
7173
await applyAwsIamUserAndGroup(awsOptions);

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: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@ const albVariablesContent = dedent`
3030
}`;
3131

3232
const albModuleContent = dedent`
33+
module "s3_access_log" {
34+
source = "../modules/s3"
35+
36+
env_namespace = local.env_namespace
37+
bucket_name = "\${local.env_namespace}-alb-access-logs-\${data.aws_caller_identity.current.account_id}"
38+
force_destroy = true
39+
}
40+
3341
module "alb" {
3442
source = "../modules/alb"
3543
@@ -39,6 +47,49 @@ const albModuleContent = dedent`
3947
subnet_ids = module.vpc.public_subnet_ids
4048
security_group_ids = module.security_group.alb_security_group_ids
4149
health_check_path = var.health_check_path
50+
bucket_access_log_name = module.s3_access_log.aws_s3_bucket_name
51+
}
52+
53+
module "s3_bucket_access_log_policy" {
54+
source = "../modules/s3BucketPolicy"
55+
56+
s3_bucket_name = module.s3_access_log.aws_s3_bucket_name
57+
s3_bucket_policy = {
58+
Version = "2012-10-17"
59+
Statement = [
60+
{
61+
Effect = "Allow"
62+
Principal = {
63+
AWS = [
64+
"\${data.aws_elb_service_account.elb_service_account.arn}"
65+
]
66+
}
67+
Action = "s3:PutObject"
68+
Resource = "arn:aws:s3:::\${module.s3_access_log.aws_s3_bucket_name}/AWSLogs/*"
69+
},
70+
{
71+
Effect = "Allow",
72+
Principal = {
73+
Service = "delivery.logs.amazonaws.com"
74+
}
75+
Action = "s3:PutObject"
76+
Resource = "arn:aws:s3:::\${module.s3_access_log.aws_s3_bucket_name}/AWSLogs/*",
77+
Condition = {
78+
StringEquals = {
79+
"s3:x-amz-acl" = "bucket-owner-full-control"
80+
}
81+
}
82+
},
83+
{
84+
Effect = "Allow",
85+
Principal = {
86+
Service = "delivery.logs.amazonaws.com"
87+
}
88+
Action = "s3:GetBucketAcl"
89+
Resource = "arn:aws:s3:::\${module.s3_access_log.aws_s3_bucket_name}"
90+
}
91+
]
92+
}
4293
}`;
4394

4495
const albOutputsContent = dedent`
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { AwsOptions } from '@/generators/addons/aws';
2+
import { remove } from '@/helpers/file';
3+
4+
import applyTerraformAwsData from './data';
5+
6+
describe('Data add-on', () => {
7+
describe('given valid AwsOptions', () => {
8+
const projectDir = 'data-addon-test';
9+
10+
beforeAll(async () => {
11+
const awsOptions: AwsOptions = {
12+
projectName: projectDir,
13+
provider: 'aws',
14+
infrastructureType: 'advanced',
15+
};
16+
17+
await applyTerraformAwsData(awsOptions);
18+
});
19+
20+
afterAll(() => {
21+
jest.clearAllMocks();
22+
remove('/', projectDir);
23+
});
24+
25+
it('creates the expected file', () => {
26+
expect(projectDir).toHaveFile('core/data.tf');
27+
});
28+
});
29+
});
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { AwsOptions } from '@/generators/addons/aws';
2+
import { AWS_TEMPLATE_PATH } from '@/generators/addons/aws/constants';
3+
import { INFRA_CORE_PATH } from '@/generators/terraform/constants';
4+
import { copy } from '@/helpers/file';
5+
6+
const applyTerraformAwsData = async (options: AwsOptions) => {
7+
copy(
8+
`${AWS_TEMPLATE_PATH}/data.tf`,
9+
`${INFRA_CORE_PATH}/data.tf`,
10+
options.projectName
11+
);
12+
};
13+
14+
export default applyTerraformAwsData;

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import applyAwsAlb from './alb';
22
import applyAwsBastion from './bastion';
33
import applyAwsCloudwatch from './cloudwatch';
4+
import applyTerraformAwsData from './core/data';
45
import applyAwsIamUserAndGroup from './core/iamUserAndGroup';
56
import applyTerraformAwsProvider from './core/provider';
67
import applyAwsRegion from './core/region';
@@ -16,6 +17,7 @@ export {
1617
applyAwsAlb,
1718
applyAwsBastion,
1819
applyTerraformAwsProvider,
20+
applyTerraformAwsData,
1921
applyAwsCloudwatch,
2022
applyAwsEcr,
2123
applyAwsEcs,

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/s3BucketPolicy/main.tf',
45+
'modules/s3BucketPolicy/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/s3BucketPolicy`,
15+
'modules/s3BucketPolicy',
16+
options.projectName
17+
);
3418
};
3519

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

templates/addons/aws/data.tf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
data "aws_caller_identity" "current" {}
2+
data "aws_elb_service_account" "elb_service_account" {}
3+
data "aws_partition" "current" {}
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+
}

0 commit comments

Comments
 (0)