Skip to content

Commit 9a0c1be

Browse files
Add tags (#17)
* Initial work * Adding additional tag generation logic * Adding additional tag merge to missed resources * Adding example to action description * Removing the merge(X+var.additional_tags), additional tags merged to default tags in provider * removing sg postfix * updating readme with `additional_tags` attribute Co-authored-by: LeoDiazL <[email protected]>
1 parent 854fca0 commit 9a0c1be

File tree

6 files changed

+29
-2
lines changed

6 files changed

+29
-2
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ jobs:
5050
tf_state_bucket: my-terraform-state-bucket
5151
dot_env: ${{ secrets.DOT_ENV }}
5252
app_port: 3000
53+
additional_tags: "{\"key1\": \"value1\",\"key2\": \"value2\"}"
5354

5455
```
5556

@@ -77,6 +78,7 @@ The following inputs can be used as `step.with` keys
7778
| `stack_destroy` | String | Set to `true` to destroy the stack. Default is `""` - Will delete the tf_state_bucket after destroy. |
7879
| `aws_resource_identifier` | String | Set to override the AWS resource identifier for the deployment. Defaults to `${org}-{repo}-{branch}`. Use with destroy to destroy specific resources. |
7980
| `app_directory` | String | Relative path for the directory of the app (i.e. where `Dockerfile` and `docker-compose.yaml` files are located). This is the directory that is copied to the EC2 instance. Default is the root of the repo. |
81+
| `additional_tags` | JSON | Add additional tags to the terraform [default tags](https://www.hashicorp.com/blog/default-tags-in-the-terraform-aws-provider), any tags put here will be added to all provisioned resources.|
8082

8183
## Note about resource identifiers
8284

action.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ inputs:
5050
description: 'Define the sub-domain part of the URL. Defaults to `${org}-${repo}-{branch}`'
5151
app_directory:
5252
description: 'Relative path for the directory of the app (i.e. where `Dockerfile` and `docker-compose.yaml` files are located). This is the directory that is copied to the EC2 instance. Default is the root of the repo.'
53+
additional_tags:
54+
description: 'A list of additional tags that will be included on created resources. Example: `{"key1": "value1", "key2": "value2"}`'
5355
outputs:
5456
vm_url:
5557
description: "The URL of the generated app"
@@ -83,6 +85,7 @@ runs:
8385
SUB_DOMAIN: ${{ inputs.sub_domain }}
8486
BITOPS_FAST_FAIL: true
8587
APP_DIRECTORY: ${{ inputs.app_directory }}
88+
ADDITIONAL_TAGS: ${{ inputs.additional_tags }}
8689
run: |
8790
echo "running operations/_scripts/deploy/deploy.sh"
8891
$GITHUB_ACTION_PATH/operations/_scripts/deploy/deploy.sh

operations/_scripts/generate/generate_provider.sh

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,12 @@ provider \"aws\" {
3333
region = \"${AWS_DEFAULT_REGION}\"
3434
profile = \"default\"
3535
default_tags {
36-
tags = local.aws_tags
36+
tags = {
37+
merge(
38+
local.aws_tags,
39+
var.additional_tags
40+
)
41+
}
3742
}
3843
}
3944
" >> "${GITHUB_ACTION_PATH}/operations/deployment/terraform/provider.tf"

operations/_scripts/generate/generate_tf_vars.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@ if [ -z "${EC2_INSTANCE_PROFILE}" ]; then
3232
EC2_INSTANCE_PROFILE="${GITHUB_IDENTIFIER}"
3333
fi
3434

35+
36+
# ADDITIONAL TAGS
37+
IFS=',' read -ra ADDR <<< "$ADDITIONAL_TAGS"
38+
for i in "${ADDR[@]}"; do
39+
40+
IFS='=' read -r key val <<< "$i"
41+
ADDITIONAL_TAGS_LIST+="\"$key\": \"$val\","
42+
done
43+
3544
echo "
3645
app_port = \"$APP_PORT\"
3746
@@ -69,4 +78,6 @@ sub_domain_name = \"${SUB_DOMAIN}\"
6978
7079
domain_name = \"${DOMAIN_NAME}\"
7180
81+
additional_tags = {$( IFS=','; echo "${ADDITIONAL_TAGS_LIST[*]}" )}
82+
7283
" >> "${GITHUB_ACTION_PATH}/operations/deployment/terraform/terraform.tfvars"

operations/deployment/terraform/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ resource "aws_instance" "server" {
2424
iam_instance_profile = aws_iam_instance_profile.ec2_profile.name
2525

2626
tags = {
27-
Name = "${var.aws_resource_identifier} - Instance"
27+
Name = "${var.aws_resource_identifier} - Instance"
2828
}
2929
}
3030

operations/deployment/terraform/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,9 @@ variable "domain_name" {
8888
description = "root domain name without any subdomains"
8989
default = ""
9090
}
91+
92+
variable "additional_tags" {
93+
type = map(string)
94+
description = "A list of strings that will be added to created resources"
95+
default = ""
96+
}

0 commit comments

Comments
 (0)