Skip to content

Commit 094ae6f

Browse files
authored
Merge pull request #77 from NHSDigital/feature/eja-eli-204-add-api-gateway
Feature/eja eli 204 add api gateway
2 parents a929fae + 682bfa1 commit 094ae6f

32 files changed

+1025
-147
lines changed

.github/workflows/github-oidc-test.yaml

Lines changed: 0 additions & 32 deletions
This file was deleted.
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
name: Github OIDC test
2+
3+
on:
4+
workflow_dispatch:
5+
inputs:
6+
environment:
7+
description: Target environment
8+
required: true
9+
type: choice
10+
options: [dev, test, preprod]
11+
12+
jobs:
13+
plan-stacks:
14+
runs-on: ubuntu-latest
15+
environment: ${{ inputs.environment }}
16+
permissions:
17+
id-token: write
18+
contents: read
19+
20+
steps:
21+
- name: "Setup Terraform"
22+
uses: hashicorp/setup-terraform@v3
23+
with:
24+
terraform_version: ${{ vars.TF_VERSION }}
25+
26+
- name: "Set up Python"
27+
uses: actions/setup-python@v5
28+
with:
29+
python-version: '3.13'
30+
31+
- name: "Checkout Repository"
32+
uses: actions/checkout@v4
33+
34+
- name: "Build lambda artefact"
35+
run: |
36+
make dependencies install-python
37+
make build
38+
- name: "Upload lambda artefact"
39+
uses: actions/upload-artifact@v4
40+
with:
41+
name: lambda
42+
path: dist/lambda.zip
43+
44+
- name: "Download Built Lambdas"
45+
uses: actions/download-artifact@v4
46+
with:
47+
name: lambda
48+
path: ./build
49+
50+
- name: "Configure AWS Credentials"
51+
uses: aws-actions/configure-aws-credentials@v4
52+
with:
53+
role-to-assume: arn:aws:iam::${{ secrets.AWS_ACCOUNT_ID }}:role/service-roles/github-actions-api-deployment-role
54+
aws-region: eu-west-2
55+
56+
- name: "Terraform Plan Stacks"
57+
env:
58+
ENVIRONMENT: "dev"
59+
WORKSPACE: "default"
60+
TF_VAR_API_CA_CERT: ${{ secrets.API_CA_CERT }}
61+
TF_VAR_API_CLIENT_CERT: ${{ secrets.API_CLIENT_CERT }}
62+
TF_VAR_API_PRIVATE_KEY_CERT: ${{ secrets.API_PRIVATE_KEY_CERT }}
63+
64+
run: |
65+
mkdir -p ./build
66+
echo "Running: make terraform env=$ENVIRONMENT workspace=$ENVIRONMENT stack=networking tf-command=plan args=\"-auto-approve\""
67+
make terraform env=$ENVIRONMENT stack=networking tf-command=plan workspace=$ENVIRONMENT
68+
echo "Running: make terraform env=$ENVIRONMENT workspace=$WORKSPACE stack=api-layer tf-command=plan args=\"-auto-approve\""
69+
make terraform env=$ENVIRONMENT stack=api-layer tf-command=plan workspace=$WORKSPACE
70+
71+
working-directory: ./infrastructure
Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,31 @@
1-
# tflint-ignore: terraform_unused_declarations
21
variable "project_name" {
32
default = "eligibility-signposting-api"
43
type = string
54
}
65

7-
# tflint-ignore: terraform_unused_declarations
86
variable "environment" {
97
description = "The purpose of the account dev/test/ref/prod or the workspace"
108
type = string
119
}
1210

13-
# tflint-ignore: terraform_unused_declarations
1411
variable "tags" {
1512
description = "A map of tags to assign to resources."
1613
type = map(string)
1714
default = {}
1815
}
16+
17+
variable "workspace" {
18+
description = "Usually the developer short code or the name of the environment."
19+
type = string
20+
}
21+
22+
variable "stack_name" {
23+
description = "The name of the stack being deployed"
24+
type = string
25+
}
26+
27+
variable "region" {
28+
type = string
29+
description = "The aws region."
30+
default = "eu-west-2"
31+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
resource "aws_api_gateway_rest_api" "api_gateway" {
2+
name = var.workspace == "default" ? "${var.api_gateway_name}-rest-api" : "${var.workspace}-${var.api_gateway_name}-rest-api"
3+
description = "The API Gateway for ${var.project_name} ${var.environment} environment"
4+
5+
disable_execute_api_endpoint = var.disable_default_endpoint # We would want to disable this if we are using a custom domain name
6+
7+
lifecycle {
8+
create_before_destroy = true
9+
}
10+
11+
tags = {
12+
Stack = var.stack_name
13+
}
14+
}
15+
16+
resource "aws_api_gateway_account" "api_gateway" {
17+
cloudwatch_role_arn = aws_iam_role.api_gateway.arn
18+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
resource "aws_cloudwatch_log_group" "api_gateway" {
2+
name = "/aws/apigateway/${var.workspace}-${var.api_gateway_name}"
3+
retention_in_days = 14
4+
tags = var.tags
5+
kms_key_id = aws_kms_key.api_gateway.arn
6+
7+
lifecycle {
8+
prevent_destroy = false
9+
}
10+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
data "aws_caller_identity" "current" {}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../_shared/default_variables.tf
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
data "aws_iam_policy_document" "assume_role" {
2+
statement {
3+
effect = "Allow"
4+
actions = ["sts:AssumeRole"]
5+
principals {
6+
type = "Service"
7+
identifiers = ["apigateway.amazonaws.com"]
8+
}
9+
}
10+
}
11+
12+
resource "aws_iam_role" "api_gateway" {
13+
name = "${var.workspace}-${var.api_gateway_name}-role"
14+
assume_role_policy = data.aws_iam_policy_document.assume_role.json
15+
}
16+
17+
data "aws_iam_policy_document" "api_gateway_logging" {
18+
statement {
19+
sid = "AllowCloudWatchLogging"
20+
effect = "Allow"
21+
actions = [
22+
"logs:CreateLogGroup",
23+
"logs:CreateLogStream",
24+
"logs:DescribeLogGroups",
25+
"logs:DescribeLogStreams",
26+
"logs:PutLogEvents",
27+
"logs:GetLogEvents",
28+
"logs:FilterLogEvents"
29+
]
30+
resources = ["*"]
31+
}
32+
}
33+
34+
resource "aws_iam_policy" "api_gateway_logging" {
35+
name = "${var.workspace}-${var.api_gateway_name}-api-gateway-logging-policy"
36+
description = "Policy to allow API Gateway push logs to Cloudwatch"
37+
policy = data.aws_iam_policy_document.api_gateway_logging.json
38+
}
39+
40+
resource "aws_iam_role_policy_attachment" "api_gateway_logging" {
41+
role = aws_iam_role.api_gateway.name
42+
policy_arn = aws_iam_policy.api_gateway_logging.arn
43+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
resource "aws_kms_key" "api_gateway" {
2+
description = "${var.workspace} - KMS Key for ${var.api_gateway_name} API Gateway"
3+
deletion_window_in_days = 14
4+
enable_key_rotation = true
5+
6+
tags = {
7+
Stack = var.stack_name
8+
}
9+
}
10+
11+
resource "aws_kms_alias" "api_gateway" {
12+
name = "alias/${var.workspace}-${var.api_gateway_name}-cloudwatch-logs"
13+
target_key_id = aws_kms_key.api_gateway.key_id
14+
}
15+
16+
resource "aws_kms_key_policy" "api_gateway" {
17+
key_id = aws_kms_key.api_gateway.id
18+
policy = data.aws_iam_policy_document.api_gateway.json
19+
}
20+
21+
data "aws_iam_policy_document" "api_gateway" {
22+
statement {
23+
sid = "Enable IAM User Permissions for ${var.api_gateway_name} API Gateway"
24+
effect = "Allow"
25+
principals {
26+
type = "AWS"
27+
identifiers = ["arn:aws:iam::${data.aws_caller_identity.current.account_id}:root"]
28+
}
29+
actions = ["kms:*"]
30+
resources = [aws_kms_key.api_gateway.arn]
31+
}
32+
statement {
33+
sid = "APIGatewayCloudwatchKMSAccess"
34+
effect = "Allow"
35+
principals {
36+
type = "Service"
37+
identifiers = ["logs.${var.region}.amazonaws.com"]
38+
}
39+
actions = [
40+
"kms:Encrypt*",
41+
"kms:Decrypt*",
42+
"kms:ReEncrypt*",
43+
"kms:GenerateDataKey*",
44+
"kms:Describe*"
45+
]
46+
resources = [aws_kms_key.api_gateway.arn]
47+
}
48+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
output "rest_api_id" {
2+
value = aws_api_gateway_rest_api.api_gateway.id
3+
}
4+
5+
output "root_resource_id" {
6+
value = aws_api_gateway_rest_api.api_gateway.root_resource_id
7+
}
8+
9+
output "execution_arn" {
10+
value = aws_api_gateway_rest_api.api_gateway.execution_arn
11+
}
12+
13+
output "cloudwatch_destination_arn" {
14+
value = aws_cloudwatch_log_group.api_gateway.arn
15+
}
16+
17+
output "api_gateway_account" {
18+
value = aws_api_gateway_account.api_gateway
19+
}
20+
21+
output "logging_policy_attachment" {
22+
value = aws_iam_role_policy_attachment.api_gateway_logging
23+
}
24+
25+
output "iam_role_name" {
26+
value = aws_iam_role.api_gateway.name
27+
}

0 commit comments

Comments
 (0)