Skip to content

Commit 77933bc

Browse files
authored
Merge pull request #6 from NHSDigital/feature/CCM-6250_shared_modules
CCM-6250: shared modules
2 parents 4c7d9e5 + 1fc4545 commit 77933bc

61 files changed

Lines changed: 1515 additions & 1 deletion

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.tool-versions

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This file is for you! Please, updated to the versions agreed by your team.
22

3-
terraform 1.9.1
3+
terraform 1.9.2
44
pre-commit 3.6.0
55
nodejs 18.18.2
66
gitleaks 8.18.4
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
resource "aws_amplify_branch" "main" {
2+
app_id = var.amplify_app_id
3+
description = var.description
4+
branch_name = var.branch
5+
display_name = var.display_name
6+
enable_pull_request_preview = var.enable_pull_request_preview
7+
enable_auto_build = var.enable_auto_build
8+
stage = var.stage
9+
framework = var.framework
10+
11+
environment_variables = var.environment_variables
12+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
locals {
2+
csi = format(
3+
"%s-%s-%s-%s-%s",
4+
var.project,
5+
var.environment,
6+
var.component,
7+
var.module,
8+
var.name,
9+
)
10+
11+
# CSI for use in resources with an account namespace, eg IAM roles
12+
csi_account = replace(
13+
format(
14+
"%s-%s-%s-%s-%s-%s",
15+
var.project,
16+
var.region,
17+
var.environment,
18+
var.component,
19+
var.module,
20+
var.name,
21+
),
22+
"_",
23+
"",
24+
)
25+
26+
default_tags = merge(
27+
var.default_tags,
28+
{
29+
Module = var.module
30+
Name = local.csi
31+
},
32+
)
33+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
output "name" {
2+
value = aws_amplify_branch.main.branch_name
3+
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
##
2+
# Basic inherited variables for terraformscaffold modules
3+
##
4+
5+
variable "project" {
6+
type = string
7+
description = "The name of the terraformscaffold project calling the module"
8+
}
9+
10+
variable "environment" {
11+
type = string
12+
description = "The name of the terraformscaffold environment the module is called for"
13+
}
14+
15+
variable "component" {
16+
type = string
17+
description = "The name of the terraformscaffold component calling this module"
18+
}
19+
20+
variable "aws_account_id" {
21+
type = string
22+
description = "The AWS Account ID (numeric)"
23+
}
24+
25+
variable "group" {
26+
type = string
27+
description = "The group variables are being inherited from (often synonmous with account short-name)"
28+
}
29+
30+
variable "description" {
31+
type = string
32+
description = "Description for the branch"
33+
}
34+
35+
##
36+
# Module self-identification
37+
##
38+
39+
variable "module" {
40+
type = string
41+
description = "The name of this module. This is a special variable, it should be set only here and never overridden."
42+
default = "kms"
43+
}
44+
45+
##
46+
# Variable specific to the module
47+
##
48+
49+
# We presume this will always be specified. The default of {} will cause an error if a valid map is not specified.
50+
# If we ever want to define this but allow it to not be specified, then we must provide a default tag keypair will be applied
51+
# as the true default. In any other case default_tags should be removed from the module.
52+
variable "default_tags" {
53+
type = map(string)
54+
description = "Default tag map for application to all taggable resources in the module"
55+
default = {}
56+
}
57+
58+
variable "region" {
59+
type = string
60+
description = "The AWS Region"
61+
}
62+
63+
variable "name" {
64+
type = string
65+
description = "A unique name to distinguish this module invocation from others within the same CSI scope"
66+
}
67+
68+
variable "amplify_app_id" {
69+
type = string
70+
description = "Amplify application ID"
71+
}
72+
73+
variable "branch" {
74+
description = "The name of the branch being deployed"
75+
type = string
76+
}
77+
78+
variable "display_name" {
79+
description = "The display name of the branch app being deployed"
80+
type = string
81+
default = null
82+
}
83+
84+
variable "enable_auto_build" {
85+
type = bool
86+
description = "Enable the auto build of the branch code as well as just the resources for it"
87+
default = false
88+
}
89+
90+
variable "enable_pull_request_preview" {
91+
type = bool
92+
description = "Enable the pull request preview"
93+
default = false
94+
}
95+
96+
variable "stage" {
97+
type = string
98+
default = null
99+
description = "Determine what stage is being deployed for"
100+
}
101+
102+
variable "framework" {
103+
type = string
104+
default = null
105+
description = "Set what framework to use"
106+
}
107+
108+
variable "environment_variables" {
109+
type = map(string)
110+
default = {}
111+
description = "Environment variables to be used for amplify branch"
112+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
terraform {
3+
required_providers {
4+
aws = {
5+
source = "hashicorp/aws"
6+
}
7+
}
8+
required_version = ">= 1.9.0"
9+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#tfsec:ignore:aws-iam-no-policy-wildcards
2+
data "aws_iam_policy_document" "admin" {
3+
policy_id = "${local.csi}-admin"
4+
5+
statement {
6+
sid = "AllowKeyAdmin"
7+
effect = "Allow"
8+
9+
actions = [
10+
"kms:Create*",
11+
"kms:Describe*",
12+
"kms:Enable*",
13+
"kms:List*",
14+
"kms:Put*",
15+
"kms:Update*",
16+
"kms:Revoke*",
17+
"kms:Disable*",
18+
"kms:Get*",
19+
"kms:Delete*",
20+
"kms:TagResource",
21+
"kms:UntagResource",
22+
"kms:ScheduleKeyDeletion",
23+
"kms:CancelKeyDeletion",
24+
]
25+
26+
resources = [
27+
aws_kms_key.main.arn,
28+
aws_kms_alias.main.arn,
29+
]
30+
}
31+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#tfsec:ignore:aws-iam-no-policy-wildcards
2+
data "aws_iam_policy_document" "user" {
3+
policy_id = "${local.csi}-user"
4+
5+
statement {
6+
sid = "AllowUseOfTheKmskey"
7+
effect = "Allow"
8+
9+
actions = [
10+
"kms:Encrypt",
11+
"kms:Decrypt",
12+
"kms:ReEncrypt*",
13+
"kms:GenerateDataKey*",
14+
"kms:DescribeKey",
15+
]
16+
17+
resources = [
18+
aws_kms_key.main.arn,
19+
]
20+
}
21+
22+
statement {
23+
sid = "AllowDelegationToAwsServiceViaGrant"
24+
effect = "Allow"
25+
26+
actions = [
27+
"kms:CreateGrant",
28+
]
29+
30+
resources = [
31+
aws_kms_key.main.arn,
32+
]
33+
34+
condition {
35+
test = "Bool"
36+
variable = "kms:GrantIsForAWSResource"
37+
38+
values = [
39+
"true",
40+
]
41+
}
42+
}
43+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
data "aws_iam_policy_document" "key" {
2+
source_policy_documents = var.key_policy_documents
3+
4+
dynamic "statement" {
5+
for_each = var.iam_delegation ? [1] : []
6+
content {
7+
sid = "AllowFullLocalAdministration"
8+
effect = "Allow"
9+
10+
principals {
11+
type = "AWS"
12+
13+
identifiers = [
14+
"arn:aws:iam::${var.aws_account_id}:root",
15+
]
16+
}
17+
18+
actions = [
19+
"kms:*",
20+
]
21+
22+
resources = [
23+
"*",
24+
]
25+
}
26+
}
27+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Create the Key Policy for the AWS KMS Key
2+
resource "aws_iam_policy" "admin" {
3+
name = "${local.csi_account}-admin"
4+
path = "/"
5+
policy = data.aws_iam_policy_document.admin.json
6+
7+
tags = merge(
8+
local.default_tags,
9+
{
10+
Name = "${local.csi_account}-admin",
11+
},
12+
)
13+
}

0 commit comments

Comments
 (0)