Skip to content

Commit 6bdc4e9

Browse files
authored
Merge pull request #44 from kunduso/use-vpc-module
Use vpc module
2 parents 75aeafe + 06d54de commit 6bdc4e9

15 files changed

+282
-140
lines changed

.github/workflows/code-scan.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ jobs:
3434
# This will add both a CLI output to the console and create a results.sarif file
3535
output_format: cli,sarif
3636
output_file_path: console,results.sarif
37+
#https://github.com/bridgecrewio/checkov/issues/6216
38+
skip_check: CKV2_GHA_1
3739

3840
- name: Upload SARIF file
3941
uses: github/codeql-action/upload-sarif@v2

.github/workflows/documentation.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#https://github.com/terraform-docs/gh-actions
2+
name: generate-terraform-docs
3+
on:
4+
- pull_request
5+
jobs:
6+
docs:
7+
runs-on: ubuntu-latest
8+
permissions:
9+
#checkov:skip=CKV2_GHA_1: This is required to add Terraform module details to the ReadMe.md
10+
# Ensure top-level permissions are not set to write-all
11+
contents: write
12+
id-token: write
13+
pull-requests: write
14+
steps:
15+
- uses: actions/checkout@v3
16+
with:
17+
ref: ${{ github.event.pull_request.head.ref }}
18+
19+
- name: Render terraform docs inside the README.md and push changes back to PR branch
20+
uses: terraform-docs/gh-actions@v1.3.0
21+
with:
22+
working-dir: .
23+
output-file: README.md
24+
output-method: inject
25+
git-push: "true"

README.md

Lines changed: 81 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
[![License: Unlicense](https://img.shields.io/badge/license-Unlicense-white.svg)](https://choosealicense.com/licenses/unlicense/)[![GitHub pull-requests closed](https://img.shields.io/github/issues-pr-closed/kunduso/amazon-elasticache-redis-tf)](https://github.com/kunduso/amazon-elasticache-redis-tf/pulls?q=is%3Apr+is%3Aclosed)[![GitHub pull-requests](https://img.shields.io/github/issues-pr/kunduso/amazon-elasticache-redis-tf)](https://GitHub.com/kunduso/amazon-elasticache-redis-tf/pull/)
2-
[![GitHub issues-closed](https://img.shields.io/github/issues-closed/kunduso/amazon-elasticache-redis-tf)](https://github.com/kunduso/amazon-elasticache-redis-tf/issues?q=is%3Aissue+is%3Aclosed)[![GitHub issues](https://img.shields.io/github/issues/kunduso/amazon-elasticache-redis-tf)](https://GitHub.com/kunduso/amazon-elasticache-redis-tf/issues/)
3-
[![terraform-infra-provisioning](https://github.com/kunduso/amazon-elasticache-redis-tf/actions/workflows/terraform.yml/badge.svg?branch=main)](https://github.com/kunduso/amazon-elasticache-redis-tf/actions/workflows/terraform.yml)[![checkov-static-analysis-scan](https://github.com/kunduso/amazon-elasticache-redis-tf/actions/workflows/code-scan.yml/badge.svg?branch=main)](https://github.com/kunduso/amazon-elasticache-redis-tf/actions/workflows/code-scan.yml)
1+
[![License: Unlicense](https://img.shields.io/badge/license-Unlicense-white.svg)](https://choosealicense.com/licenses/unlicense/) [![GitHub pull-requests closed](https://img.shields.io/github/issues-pr-closed/kunduso/amazon-elasticache-redis-tf)](https://github.com/kunduso/amazon-elasticache-redis-tf/pulls?q=is%3Apr+is%3Aclosed) [![GitHub pull-requests](https://img.shields.io/github/issues-pr/kunduso/amazon-elasticache-redis-tf)](https://GitHub.com/kunduso/amazon-elasticache-redis-tf/pull/)
2+
[![GitHub issues-closed](https://img.shields.io/github/issues-closed/kunduso/amazon-elasticache-redis-tf)](https://github.com/kunduso/amazon-elasticache-redis-tf/issues?q=is%3Aissue+is%3Aclosed) [![GitHub issues](https://img.shields.io/github/issues/kunduso/amazon-elasticache-redis-tf)](https://GitHub.com/kunduso/amazon-elasticache-redis-tf/issues/)
3+
[![terraform-infra-provisioning](https://github.com/kunduso/amazon-elasticache-redis-tf/actions/workflows/terraform.yml/badge.svg?branch=main)](https://github.com/kunduso/amazon-elasticache-redis-tf/actions/workflows/terraform.yml) [![checkov-static-analysis-scan](https://github.com/kunduso/amazon-elasticache-redis-tf/actions/workflows/code-scan.yml/badge.svg?branch=main)](https://github.com/kunduso/amazon-elasticache-redis-tf/actions/workflows/code-scan.yml) [![Generate terraform docs](https://github.com/kunduso/amazon-elasticache-redis-tf/actions/workflows/documentation.yml/badge.svg)](https://github.com/kunduso/amazon-elasticache-redis-tf/actions/workflows/documentation.yml)
44

55

66
![Image](https://skdevops.files.wordpress.com/2023/12/87-image-0-1.png)
@@ -28,10 +28,88 @@ For this code to function without errors, I created an **OpenID connect** identi
2828
<br />I stored the ARN of the IAM Role as a GitHub secret which is referred in the [`terraform.yml`](https://github.com/kunduso/amazon-elasticache-redis-tf/blob/eb148db2b9ff37cff9f1fb469d0c14b6479bd57a/.github/workflows/terraform.yml#L42) file.
2929
<br />Since I used Infracost in this repository, I stored the `INFRACOST_API_KEY` as a repository secret. It is referenced in the [`terraform.yml`](https://github.com/kunduso/amazon-elasticache-redis-tf/blob/eb148db2b9ff37cff9f1fb469d0c14b6479bd57a/.github/workflows/terraform.yml#L52) GitHub actions workflow file.
3030
<br />As part of the Infracost integration, I also created a `INFRACOST_API_KEY` and stored that as a GitHub Actions secret. I also managed the cost estimate process using a GitHub Actions variable `INFRACOST_SCAN_TYPE` where the value is either `hcl_code` or `tf_plan`, depending on the type of scan desired.
31+
<!-- BEGIN_TF_DOCS -->
32+
## Requirements
33+
34+
| Name | Version |
35+
|------|---------|
36+
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | 5.20.1 |
37+
| <a name="requirement_random"></a> [random](#requirement\_random) | 3.6.3 |
38+
39+
## Providers
40+
41+
| Name | Version |
42+
|------|---------|
43+
| <a name="provider_aws"></a> [aws](#provider\_aws) | 5.20.1 |
44+
| <a name="provider_random"></a> [random](#provider\_random) | 3.6.3 |
45+
46+
## Modules
47+
48+
| Name | Source | Version |
49+
|------|--------|---------|
50+
| <a name="module_vpc"></a> [vpc](#module\_vpc) | github.com/kunduso/terraform-aws-vpc | v1.0.1 |
51+
52+
## Resources
53+
54+
| Name | Type |
55+
|------|------|
56+
| [aws_cloudwatch_log_group.engine_log](https://registry.terraform.io/providers/hashicorp/aws/5.20.1/docs/resources/cloudwatch_log_group) | resource |
57+
| [aws_cloudwatch_log_group.slow_log](https://registry.terraform.io/providers/hashicorp/aws/5.20.1/docs/resources/cloudwatch_log_group) | resource |
58+
| [aws_elasticache_replication_group.app4](https://registry.terraform.io/providers/hashicorp/aws/5.20.1/docs/resources/elasticache_replication_group) | resource |
59+
| [aws_elasticache_subnet_group.elasticache_subnet](https://registry.terraform.io/providers/hashicorp/aws/5.20.1/docs/resources/elasticache_subnet_group) | resource |
60+
| [aws_iam_instance_profile.ec2_profile](https://registry.terraform.io/providers/hashicorp/aws/5.20.1/docs/resources/iam_instance_profile) | resource |
61+
| [aws_iam_policy.secret_manager_policy](https://registry.terraform.io/providers/hashicorp/aws/5.20.1/docs/resources/iam_policy) | resource |
62+
| [aws_iam_policy.ssm_parameter_policy](https://registry.terraform.io/providers/hashicorp/aws/5.20.1/docs/resources/iam_policy) | resource |
63+
| [aws_iam_role.ec2_role](https://registry.terraform.io/providers/hashicorp/aws/5.20.1/docs/resources/iam_role) | resource |
64+
| [aws_iam_role_policy_attachment.custom](https://registry.terraform.io/providers/hashicorp/aws/5.20.1/docs/resources/iam_role_policy_attachment) | resource |
65+
| [aws_iam_role_policy_attachment.secret_policy_attachement](https://registry.terraform.io/providers/hashicorp/aws/5.20.1/docs/resources/iam_role_policy_attachment) | resource |
66+
| [aws_iam_role_policy_attachment.ssm_policy_attachement](https://registry.terraform.io/providers/hashicorp/aws/5.20.1/docs/resources/iam_role_policy_attachment) | resource |
67+
| [aws_instance.app-server-read](https://registry.terraform.io/providers/hashicorp/aws/5.20.1/docs/resources/instance) | resource |
68+
| [aws_instance.app-server-write](https://registry.terraform.io/providers/hashicorp/aws/5.20.1/docs/resources/instance) | resource |
69+
| [aws_kms_alias.encryption_rest](https://registry.terraform.io/providers/hashicorp/aws/5.20.1/docs/resources/kms_alias) | resource |
70+
| [aws_kms_alias.encryption_secret](https://registry.terraform.io/providers/hashicorp/aws/5.20.1/docs/resources/kms_alias) | resource |
71+
| [aws_kms_key.encryption_rest](https://registry.terraform.io/providers/hashicorp/aws/5.20.1/docs/resources/kms_key) | resource |
72+
| [aws_kms_key.encryption_secret](https://registry.terraform.io/providers/hashicorp/aws/5.20.1/docs/resources/kms_key) | resource |
73+
| [aws_kms_key_policy.encryption_rest_policy](https://registry.terraform.io/providers/hashicorp/aws/5.20.1/docs/resources/kms_key_policy) | resource |
74+
| [aws_kms_key_policy.encryption_secret_policy](https://registry.terraform.io/providers/hashicorp/aws/5.20.1/docs/resources/kms_key_policy) | resource |
75+
| [aws_secretsmanager_secret.elasticache_auth](https://registry.terraform.io/providers/hashicorp/aws/5.20.1/docs/resources/secretsmanager_secret) | resource |
76+
| [aws_secretsmanager_secret_version.auth](https://registry.terraform.io/providers/hashicorp/aws/5.20.1/docs/resources/secretsmanager_secret_version) | resource |
77+
| [aws_security_group.ec2_instance](https://registry.terraform.io/providers/hashicorp/aws/5.20.1/docs/resources/security_group) | resource |
78+
| [aws_security_group.elasticache](https://registry.terraform.io/providers/hashicorp/aws/5.20.1/docs/resources/security_group) | resource |
79+
| [aws_security_group_rule.ec2_instance_egress](https://registry.terraform.io/providers/hashicorp/aws/5.20.1/docs/resources/security_group_rule) | resource |
80+
| [aws_security_group_rule.ec2_instance_ingress](https://registry.terraform.io/providers/hashicorp/aws/5.20.1/docs/resources/security_group_rule) | resource |
81+
| [aws_security_group_rule.elasticache_egress](https://registry.terraform.io/providers/hashicorp/aws/5.20.1/docs/resources/security_group_rule) | resource |
82+
| [aws_security_group_rule.elasticache_ingress](https://registry.terraform.io/providers/hashicorp/aws/5.20.1/docs/resources/security_group_rule) | resource |
83+
| [aws_ssm_parameter.elasticache_ep](https://registry.terraform.io/providers/hashicorp/aws/5.20.1/docs/resources/ssm_parameter) | resource |
84+
| [aws_ssm_parameter.elasticache_port](https://registry.terraform.io/providers/hashicorp/aws/5.20.1/docs/resources/ssm_parameter) | resource |
85+
| [random_password.auth](https://registry.terraform.io/providers/hashicorp/random/3.6.3/docs/resources/password) | resource |
86+
| [aws_ami.amazon_ami](https://registry.terraform.io/providers/hashicorp/aws/5.20.1/docs/data-sources/ami) | data source |
87+
| [aws_caller_identity.current](https://registry.terraform.io/providers/hashicorp/aws/5.20.1/docs/data-sources/caller_identity) | data source |
88+
89+
## Inputs
90+
91+
| Name | Description | Type | Default | Required |
92+
|------|-------------|------|---------|:--------:|
93+
| <a name="input_access_key"></a> [access\_key](#input\_access\_key) | The access\_key that belongs to the IAM user. | `string` | `""` | no |
94+
| <a name="input_ami_name"></a> [ami\_name](#input\_ami\_name) | The ami name of the image from where the instances will be created | `list(string)` | <pre>[<br/> "amzn2-ami-amd-hvm-2.0.20230727.0-x86_64-gp2"<br/>]</pre> | no |
95+
| <a name="input_instance_type"></a> [instance\_type](#input\_instance\_type) | The instance type of the EC2 instances | `string` | `"t3.medium"` | no |
96+
| <a name="input_name"></a> [name](#input\_name) | The name of the application. | `string` | `"app-4"` | no |
97+
| <a name="input_region"></a> [region](#input\_region) | AWS Cloud infrastructure region. | `string` | `"us-east-2"` | no |
98+
| <a name="input_secret_key"></a> [secret\_key](#input\_secret\_key) | The secret\_key that belongs to the IAM user. | `string` | `""` | no |
99+
| <a name="input_subnet_cidr_private"></a> [subnet\_cidr\_private](#input\_subnet\_cidr\_private) | CIDR blocks for the private subnets. | `list(any)` | <pre>[<br/> "10.20.32.0/27",<br/> "10.20.32.32/27",<br/> "10.20.32.64/27"<br/>]</pre> | no |
100+
| <a name="input_subnet_cidr_public"></a> [subnet\_cidr\_public](#input\_subnet\_cidr\_public) | CIDR blocks for the public subnets. | `list(any)` | <pre>[<br/> "10.20.32.96/27"<br/>]</pre> | no |
101+
| <a name="input_vpc_cidr"></a> [vpc\_cidr](#input\_vpc\_cidr) | CIDR for the VPC. | `string` | `"10.20.32.0/25"` | no |
102+
103+
## Outputs
104+
105+
No outputs.
106+
<!-- END_TF_DOCS -->
31107
## Usage
32108
Ensure that the policy attached to the IAM role whose credentials are being used in this configuration has permission to create and manage all the resources that are included in this repository.
33109

34110
<br />Review the code including the [`terraform.yml`](./.github/workflows/terraform.yml) to understand the steps in the GitHub Actions pipeline. Also review the terraform code to understand all the concepts associated with creating an AWS VPC, subnets, internet gateway, route table, and route table association.
35111
<br />If you want to check the pipeline logs, click on the **Build Badge** (terrform-infra-provisioning) above the image in this ReadMe.
112+
## Contributing
113+
If you find any issues or have suggestions for improvement, feel free to open an issue or submit a pull request. Contributions are always welcome!
36114
## License
37115
This code is released under the Unlincse License. See [LICENSE](LICENSE).

cloudwatch.tf

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
#https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_log_group
12
resource "aws_cloudwatch_log_group" "slow_log" {
2-
name = "/elasticache/${var.replication_group_id}/slow-log"
3+
name = "/elasticache/${var.name}/slow-log"
34
retention_in_days = 365
45
kms_key_id = aws_kms_key.encryption_rest.arn
56
}
7+
#https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_log_group
68
resource "aws_cloudwatch_log_group" "engine_log" {
7-
name = "/elasticache/${var.replication_group_id}/engine-log"
9+
name = "/elasticache/${var.name}/engine-log"
810
retention_in_days = 365
911
kms_key_id = aws_kms_key.encryption_rest.arn
1012
}

data.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ data "aws_caller_identity" "current" {}
22
locals {
33
principal_root_arn = "arn:aws:iam::${data.aws_caller_identity.current.account_id}:root"
44
principal_logs_arn = "logs.${var.region}.amazonaws.com"
5-
slow_log_arn = "arn:aws:logs:${var.region}:${data.aws_caller_identity.current.account_id}:log-group:/elasticache/${var.replication_group_id}/slow-log"
6-
engine_log_arn = "arn:aws:logs:${var.region}:${data.aws_caller_identity.current.account_id}:log-group:/elasticache/${var.replication_group_id}/engine-log"
5+
slow_log_arn = "arn:aws:logs:${var.region}:${data.aws_caller_identity.current.account_id}:log-group:/elasticache/${var.name}/slow-log"
6+
engine_log_arn = "arn:aws:logs:${var.region}:${data.aws_caller_identity.current.account_id}:log-group:/elasticache/${var.name}/engine-log"
77
}

ec2.tf

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,32 @@
1-
resource "aws_internet_gateway" "this-igw" {
2-
vpc_id = aws_vpc.this.id
3-
tags = {
4-
"Name" = "app-4-gateway"
5-
}
6-
}
7-
resource "aws_route" "internet-route" {
8-
destination_cidr_block = "0.0.0.0/0"
9-
route_table_id = aws_route_table.public.id
10-
gateway_id = aws_internet_gateway.this-igw.id
11-
}
121
# create a security group
2+
#https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group
133
resource "aws_security_group" "ec2_instance" {
14-
name = "app-4-ec2"
4+
name = "${var.name}-ec2"
155
description = "Allow inbound to and outbound access from the Amazon EC2 instance."
16-
ingress {
17-
from_port = 0
18-
to_port = 0
19-
protocol = "-1"
20-
cidr_blocks = [var.vpc_cidr]
21-
description = "Enable access from any resource inside the VPC."
22-
}
23-
egress {
24-
from_port = 0
25-
to_port = 0
26-
protocol = "-1"
27-
cidr_blocks = ["0.0.0.0/0"]
28-
description = "Enable access to the internet."
29-
}
30-
vpc_id = aws_vpc.this.id
6+
vpc_id = module.vpc.vpc.id
7+
}
8+
#https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group_rule
9+
resource "aws_security_group_rule" "ec2_instance_ingress" {
10+
type = "ingress"
11+
security_group_id = aws_security_group.ec2_instance.id
12+
from_port = 0
13+
to_port = 0
14+
protocol = "-1"
15+
cidr_blocks = [var.vpc_cidr]
16+
description = "Enable access from any resource inside the VPC."
17+
}
18+
#https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group_rule
19+
resource "aws_security_group_rule" "ec2_instance_egress" {
20+
type = "egress"
21+
security_group_id = aws_security_group.ec2_instance.id
22+
from_port = 0
23+
to_port = 0
24+
protocol = "-1"
25+
cidr_blocks = ["0.0.0.0/0"]
26+
description = "Enable access to the internet."
3127
}
3228

33-
#create an EC2 in a public subnet
29+
#https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/ami
3430
data "aws_ami" "amazon_ami" {
3531
filter {
3632
name = "name"
@@ -43,14 +39,16 @@ data "aws_ami" "amazon_ami" {
4339
most_recent = true
4440
owners = ["amazon"]
4541
}
42+
#create an EC2 in a public subnet
43+
#https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/instance
4644
resource "aws_instance" "app-server-read" {
4745
instance_type = var.instance_type
4846
ami = data.aws_ami.amazon_ami.id
4947
vpc_security_group_ids = [aws_security_group.ec2_instance.id]
5048
iam_instance_profile = aws_iam_instance_profile.ec2_profile.name
5149
associate_public_ip_address = true
5250
#checkov:skip=CKV_AWS_88: Required for Session Manager access
53-
subnet_id = aws_subnet.public[0].id
51+
subnet_id = module.vpc.private_subnets[0].id
5452
ebs_optimized = true
5553
monitoring = true
5654
root_block_device {
@@ -61,7 +59,7 @@ resource "aws_instance" "app-server-read" {
6159
http_tokens = "required"
6260
}
6361
tags = {
64-
Name = "app-4-server-read"
62+
Name = "${var.name}-server-read"
6563
}
6664
user_data = templatefile("user_data/read_elasticache.tpl",
6765
{
@@ -71,14 +69,15 @@ resource "aws_instance" "app-server-read" {
7169
elasticache_auth_token = aws_secretsmanager_secret.elasticache_auth.name
7270
})
7371
}
72+
#https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/instance
7473
resource "aws_instance" "app-server-write" {
7574
instance_type = var.instance_type
7675
ami = data.aws_ami.amazon_ami.id
7776
vpc_security_group_ids = [aws_security_group.ec2_instance.id]
7877
iam_instance_profile = aws_iam_instance_profile.ec2_profile.name
7978
associate_public_ip_address = true
8079
#checkov:skip=CKV_AWS_88: Required for Session Manager access
81-
subnet_id = aws_subnet.public[0].id
80+
subnet_id = module.vpc.private_subnets[0].id
8281
ebs_optimized = true
8382
monitoring = true
8483
root_block_device {
@@ -89,7 +88,7 @@ resource "aws_instance" "app-server-write" {
8988
http_tokens = "required"
9089
}
9190
tags = {
92-
Name = "app-4-server-write"
91+
Name = "${var.name}-server-write"
9392
}
9493
user_data = templatefile("user_data/write_elasticache.tpl",
9594
{

ec2_role.tf

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# #https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role
22
resource "aws_iam_role" "ec2_role" {
3-
name = "app-4-ec2-role"
4-
3+
name = "${var.name}-ec2-role"
54
# Terraform's "jsonencode" function converts a
65
# Terraform expression result to valid JSON syntax.
76
assume_role_policy = jsonencode({
@@ -24,18 +23,19 @@ resource "aws_iam_role_policy_attachment" "custom" {
2423
role = aws_iam_role.ec2_role.name
2524
policy_arn = "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore"
2625
}
27-
26+
#https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment
2827
resource "aws_iam_role_policy_attachment" "ssm_policy_attachement" {
2928
role = aws_iam_role.ec2_role.name
3029
policy_arn = aws_iam_policy.ssm_parameter_policy.arn
3130
}
31+
##https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment
3232
resource "aws_iam_role_policy_attachment" "secret_policy_attachement" {
3333
role = aws_iam_role.ec2_role.name
3434
policy_arn = aws_iam_policy.secret_manager_policy.arn
3535
}
3636
#Attach role to an instance profile
3737
#https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_instance_profile
3838
resource "aws_iam_instance_profile" "ec2_profile" {
39-
name = "app-4-ec2-profile"
39+
name = "${var.name}-ec2-profile"
4040
role = aws_iam_role.ec2_role.name
4141
}

0 commit comments

Comments
 (0)