Skip to content

Commit

Permalink
Merge pull request #8866 from ministryofjustice/TM-756
Browse files Browse the repository at this point in the history
TM-756 updating lambda.tf file to provision s3 bucket with db scripts…
  • Loading branch information
mnasr-moj authored Dec 4, 2024
2 parents 86b7733 + 91d52e3 commit a105515
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
3 changes: 3 additions & 0 deletions terraform/environments/apex/ec2.tf
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ resource "aws_instance" "apex_db_instance" {
user_data_base64 = base64encode(local.database-instance-userdata)
user_data_replace_on_change = true

lifecycle {
ignore_changes = [user_data_base64]
}

root_block_device {
delete_on_termination = false
Expand Down
23 changes: 15 additions & 8 deletions terraform/environments/apex/lambda.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ locals {
create_db_snapshots_script_prefix = "dbsnapshot"
delete_db_snapshots_script_prefix = "deletesnapshots"
db_connect_script_prefix = "dbconnect"
hash_value = "Y/4+i1hcHvLBzOaCHJ/m9bQLuVtQwr8gnF//AJ2j+S4="
}

resource "aws_ssm_parameter" "ssh_key" {
Expand Down Expand Up @@ -98,6 +99,11 @@ resource "aws_iam_role_policy_attachment" "backup_lambda" {
### S3 for Backup Lambda
##################################

data "aws_s3_object" "nodejs_zip" {
bucket = aws_s3_bucket.backup_lambda.id
key = "nodejs.zip"
}

resource "aws_s3_bucket" "backup_lambda" {
bucket = "${local.application_name}-${local.environment}-backup-lambda"
tags = merge(
Expand All @@ -108,11 +114,11 @@ resource "aws_s3_bucket" "backup_lambda" {

resource "aws_s3_object" "provision_files" {
bucket = aws_s3_bucket.backup_lambda.id
for_each = fileset("./zipfiles/", "**")
for_each = toset(["${local.create_db_snapshots_script_prefix}.zip", "${local.delete_db_snapshots_script_prefix}.zip", "${local.db_connect_script_prefix}.zip"])
key = each.value
source = "./zipfiles/${each.value}"
source = "./scripts/${each.value}"
content_type = "application/zip"
source_hash = filemd5("./zipfiles/${each.value}")
source_hash = filemd5("./scripts/${each.value}")
}

# This delays the creation of resource
Expand Down Expand Up @@ -161,19 +167,19 @@ resource "aws_s3_bucket_versioning" "backup_lambda" {
data "archive_file" "create_db_snapshots" {
type = "zip"
source_file = "scripts/${local.create_db_snapshots_script_prefix}.js"
output_path = "zipfiles/${local.create_db_snapshots_script_prefix}.zip"
output_path = "scripts/${local.create_db_snapshots_script_prefix}.zip"
}

data "archive_file" "delete_db_snapshots" {
type = "zip"
source_file = "scripts/${local.delete_db_snapshots_script_prefix}.py"
output_path = "zipfiles/${local.delete_db_snapshots_script_prefix}.zip"
output_path = "scripts/${local.delete_db_snapshots_script_prefix}.zip"
}

data "archive_file" "connect_db" {
type = "zip"
source_file = "scripts/${local.db_connect_script_prefix}.js"
output_path = "zipfiles/${local.db_connect_script_prefix}.zip"
output_path = "scripts/${local.db_connect_script_prefix}.zip"
}


Expand Down Expand Up @@ -206,8 +212,9 @@ resource "aws_lambda_layer_version" "backup_lambda" {
license_info = "Apache-2.0"
s3_bucket = aws_s3_bucket.backup_lambda.id
s3_key = "nodejs.zip"
source_code_hash = filebase64sha256("zipfiles/nodejs.zip")

source_code_hash = local.hash_value
# Since the nodejs.zip file has been added manually to the s3 bucket the source_code_hash would have to be computed and added manually as well anytime there's a change to nodejs.zip
# This command allows you to retrieve the hash - openssl dgst -sha256 -binary nodejs.zip | base64
compatible_runtimes = ["nodejs18.x"]
depends_on = [time_sleep.wait_for_provision_files] # This resource creation will be delayed to ensure object exists in the bucket
}
Expand Down
Binary file removed terraform/environments/apex/zipfiles/nodejs.zip
Binary file not shown.

0 comments on commit a105515

Please sign in to comment.