Skip to content

Commit 2733cef

Browse files
authored
Merge pull request #51 from bitovi/42-all-aws-resources-should-have-tagsname
Adding additional_tags and tagging
2 parents 104f44a + ce2f5f3 commit 2733cef

File tree

9 files changed

+74
-64
lines changed

9 files changed

+74
-64
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ The following inputs can be used as `steps.with` keys:
7575
| `aws_ec2_instance_profile` | string | | [The AWS IAM instance profile](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_use_switch-role-ec2_instance-profiles.html) to use for the EC2 instance. Use if you want to pass an AWS role with specific permissions granted to the instance |
7676
| `aws_resource_identifier` | string | `${org}-${repo}-${branch}` | Auto-generated by default so it's unique for org/repo/branch. Set to override with custom naming the unique AWS resource identifier for the deployment. |
7777
| `aws_create_vpc` | bool | `false` | Whether an AWS VPC should be created in the action. Otherwise, the existing default VPC will be used. |
78+
| `aws_extra_tags` | json | | A list of additional tags that will be included on created resources. Example: `{"key1": "value1", "key2": "value2"}`. |
7879
| `infrastructure_only` | bool | `false` | Set to true to provision infrastructure (with Terraform) but skip the app deployment (with ansible) |
7980
| **Teraform configuration** |
8081
| `tf_state_bucket` | string | `${org}-${repo}-${branch}-tf-state` | AWS S3 bucket to use for Terraform state. By default, a new deployment will be created for each unique branch. Hardcode if you want to keep a shared resource state between the several branches. |

action.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,15 @@ inputs:
3333
aws_create_vpc:
3434
description: 'Bool, whether an AWS VPC should be created in the action. Otherwise, the existing default VPC will be used.'
3535
default: "false"
36+
aws_extra_tags:
37+
description: 'A list of additional tags that will be included on created resources. Example: `{"key1": "value1", "key2": "value2"}`'
38+
required: false
39+
default: '{}'
3640
infrastructure_only:
3741
description: 'Set to true to provision infrastructure (with Terraform) but skip the app deployment (with ansible)'
3842
default: "false"
3943

44+
4045
# Terraform configuration
4146
tf_state_bucket:
4247
description: 'AWS S3 bucket to use for Terraform state. Defaults to `${org}-${repo}-${branch}-tf-state`'
@@ -94,6 +99,8 @@ runs:
9499
STACK_DESTROY: ${{ inputs.tf_stack_destroy }}
95100
AWS_RESOURCE_IDENTIFIER: ${{ inputs.aws_resource_identifier }}
96101
CREATE_VPC: ${{ inputs.aws_create_vpc }}
102+
AWS_EXTRA_TAGS: ${{ inputs.aws_extra_tags }}
103+
97104

98105
# Skip ansible deployment if deploying only infrastructure
99106
ANSIBLE_SKIP_DEPLOY: ${{ inputs.infrastructure_only }}

operations/_scripts/generate/generate_provider.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ provider \"aws\" {
3333
region = \"${AWS_DEFAULT_REGION}\"
3434
profile = \"default\"
3535
default_tags {
36-
tags = local.aws_tags
36+
tags = merge(
37+
local.aws_tags,
38+
var.aws_extra_tags
39+
)
3740
}
3841
}
3942
" > "${GITHUB_ACTION_PATH}/operations/deployment/terraform/provider.tf"

operations/_scripts/generate/generate_tf_vars.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,5 +84,7 @@ region = \"${AWS_DEFAULT_REGION}\"
8484
# Route53
8585
route53_zone_id = \"${ROUTE53_ZONE_ID}\"
8686
87+
aws_additional_tags = ${AWS_ADDITIONAL_TAGS}
88+
8789
8890
" > "${GITHUB_ACTION_PATH}/operations/deployment/terraform/terraform.tfvars"
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
locals {
22
aws_tags = {
3-
OperationsRepo = "bitovi/github-actions-node-app-to-aws-vm/operations/${var.ops_repo_environment}"
4-
AWSResourceIdentifier = "${var.aws_resource_identifier}"
5-
GitHubOrgName = "${var.app_org_name}"
6-
GitHubRepoName = "${var.app_repo_name}"
7-
GitHubBranchName = "${var.app_branch_name}"
8-
GitHubAction = "bitovi/github-actions-node-app-to-aws-vm"
3+
OperationsRepo = "bitovi/github-actions-deploy-stackstorm/operations/${var.ops_repo_environment}"
4+
AWSResourceIdentifier = "${var.aws_resource_identifier}"
5+
GitHubOrgName = "${var.app_org_name}"
6+
GitHubRepoName = "${var.app_repo_name}"
7+
GitHubBranchName = "${var.app_branch_name}"
8+
GitHubAction = "bitovi/github-actions-deploy-stackstorm"
99
OperationsRepoEnvironment = "deployment"
10-
CreatedWith = "terraform"
10+
CreatedWith = "terraform"
1111
}
12-
}
12+
}

operations/deployment/terraform/modules/02_networking.tf

Lines changed: 43 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -59,62 +59,52 @@ resource "aws_route_table_association" "public" {
5959
}
6060

6161

62+
resource "aws_security_group" "ec2_security_group" {
63+
name = "${var.aws_resource_identifier_supershort}-SG"
64+
description = "SG for ${var.aws_resource_identifier}"
65+
vpc_id = var.create_vpc == "true" ? aws_vpc.main[0].id : null
66+
egress {
67+
from_port = 0
68+
to_port = 0
69+
protocol = "-1"
70+
cidr_blocks = ["0.0.0.0/0"]
71+
}
72+
tags = {
73+
Name = "${var.aws_resource_identifier}-instance-sg"
74+
}
75+
}
6276

77+
data "aws_security_group" "ec2_security_group" {
78+
count = var.create_vpc == "true" ? 1 : 0
79+
id = aws_security_group.ec2_security_group.id
80+
}
6381

64-
65-
resource "aws_security_group" "allow_http" {
66-
name = "${var.aws_resource_identifier_supershort}-http"
67-
description = "Allow HTTP traffic"
68-
vpc_id = var.create_vpc == "true" ? aws_vpc.main[0].id : null
69-
ingress {
70-
description = "HTTP"
71-
from_port = 80
72-
to_port = 80
73-
protocol = "tcp"
74-
cidr_blocks = ["0.0.0.0/0"]
75-
}
76-
egress {
77-
from_port = 0
78-
to_port = 0
79-
protocol = "-1"
80-
cidr_blocks = ["0.0.0.0/0"]
81-
}
82+
resource "aws_security_group_rule" "ingress_http" {
83+
type = "ingress"
84+
description = "Allow HTTP"
85+
from_port = 80
86+
to_port = 80
87+
protocol = "tcp"
88+
cidr_blocks = ["0.0.0.0/0"]
89+
security_group_id = aws_security_group.ec2_security_group.id
8290
}
83-
84-
resource "aws_security_group" "allow_https" {
85-
name = "${var.aws_resource_identifier_supershort}-https"
86-
description = "Allow HTTPS traffic"
87-
vpc_id = var.create_vpc == "true" ? aws_vpc.main[0].id : null
88-
ingress {
89-
description = "HTTPS"
90-
from_port = 443
91-
to_port = 443
92-
protocol = "tcp"
93-
cidr_blocks = ["0.0.0.0/0"]
94-
}
95-
egress {
96-
from_port = 0
97-
to_port = 0
98-
protocol = "-1"
99-
cidr_blocks = ["0.0.0.0/0"]
100-
}
91+
92+
resource "aws_security_group_rule" "ingress_https" {
93+
type = "ingress"
94+
description = "Allow HTTPS"
95+
from_port = 443
96+
to_port = 443
97+
protocol = "tcp"
98+
cidr_blocks = ["0.0.0.0/0"]
99+
security_group_id = aws_security_group.ec2_security_group.id
101100
}
102101

103-
resource "aws_security_group" "allow_ssh" {
104-
name = "${var.aws_resource_identifier_supershort}-ssh"
105-
description = "Allow SSH traffic"
106-
vpc_id = var.create_vpc == "true" ? aws_vpc.main[0].id : null
107-
ingress {
108-
description = "SSH"
109-
from_port = 22
110-
to_port = 22
111-
protocol = "tcp"
112-
cidr_blocks = ["0.0.0.0/0"]
113-
}
114-
egress {
115-
from_port = 0
116-
to_port = 0
117-
protocol = "-1"
118-
cidr_blocks = ["0.0.0.0/0"]
119-
}
102+
resource "aws_security_group_rule" "ingress_ssh" {
103+
type = "ingress"
104+
description = "Allow SSH"
105+
from_port = 22
106+
to_port = 22
107+
protocol = "tcp"
108+
cidr_blocks = ["0.0.0.0/0"]
109+
security_group_id = aws_security_group.ec2_security_group.id
120110
}

operations/deployment/terraform/modules/03_ec2.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ resource "aws_instance" "server" {
55
associate_public_ip_address = true
66

77
subnet_id = var.create_vpc == "true" ? aws_subnet.public.*.id[0] : null
8-
vpc_security_group_ids = [aws_security_group.allow_http.id, aws_security_group.allow_https.id, aws_security_group.allow_ssh.id]
8+
vpc_security_group_ids = [aws_security_group.ec2_security_group.id]
99
user_data = <<EOF
1010
#!/bin/bash
1111
echo "symlink for python3 -> python"

operations/deployment/terraform/modules/04_elb.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ resource "aws_elb" "vm" {
33
subnets = var.create_vpc == "true" ? aws_subnet.public.*.id : null
44
availability_zones = var.create_vpc == "true" ? null : [aws_instance.server.availability_zone]
55

6-
security_groups = [aws_security_group.allow_http.id, aws_security_group.allow_https.id]
6+
security_groups = [aws_security_group.ec2_security_group.id]
77

88
listener {
99
instance_port = 443

operations/deployment/terraform/variables.tf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,13 @@ variable "route53_zone_id" {
138138
type = string
139139
}
140140

141+
variable "aws_extra_tags" {
142+
type = map(string)
143+
description = "A list of tags that will be added to created resources"
144+
default = {}
145+
146+
}
147+
141148
# variable "common_tags" {
142149
# default = {}
143150
# description = "Common resource tags"

0 commit comments

Comments
 (0)