|
| 1 | +provider "aws" { |
| 2 | + region = "eu-west-1" |
| 3 | + |
| 4 | + # Make it faster by skipping something |
| 5 | + skip_get_ec2_platforms = true |
| 6 | + skip_metadata_api_check = true |
| 7 | + skip_region_validation = true |
| 8 | + skip_credentials_validation = true |
| 9 | + skip_requesting_account_id = true |
| 10 | +} |
| 11 | + |
| 12 | +################################################################################ |
| 13 | +# Lambda Function |
| 14 | +################################################################################ |
| 15 | + |
| 16 | +module "lambda" { |
| 17 | + source = "../../" |
| 18 | + |
| 19 | + function_name = random_pet.this.id |
| 20 | + handler = "index.lambda_handler" |
| 21 | + runtime = "python3.8" |
| 22 | + code_signing_config_arn = aws_lambda_code_signing_config.this.arn |
| 23 | + create_package = false |
| 24 | + |
| 25 | + s3_existing_package = { |
| 26 | + bucket = aws_signer_signing_job.this.signed_object[0].s3[0].bucket |
| 27 | + key = aws_signer_signing_job.this.signed_object[0].s3[0].key |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +################################################################################ |
| 32 | +# Lambda Code Signing |
| 33 | +################################################################################ |
| 34 | + |
| 35 | +resource "aws_s3_object" "unsigned" { |
| 36 | + bucket = module.s3_bucket.s3_bucket_id |
| 37 | + key = "unsigned/existing_package.zip" |
| 38 | + source = "${path.module}/../fixtures/python3.8-zip/existing_package.zip" |
| 39 | + |
| 40 | + # Making sure that S3 versioning configuration is propagated properly |
| 41 | + depends_on = [ |
| 42 | + module.s3_bucket |
| 43 | + ] |
| 44 | +} |
| 45 | + |
| 46 | +resource "aws_signer_signing_profile" "this" { |
| 47 | + platform_id = "AWSLambda-SHA384-ECDSA" |
| 48 | + # invalid value for name (must be alphanumeric with max length of 64 characters) |
| 49 | + name = replace(random_pet.this.id, "-", "") |
| 50 | + |
| 51 | + signature_validity_period { |
| 52 | + value = 3 |
| 53 | + type = "MONTHS" |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +resource "aws_signer_signing_job" "this" { |
| 58 | + profile_name = aws_signer_signing_profile.this.name |
| 59 | + |
| 60 | + source { |
| 61 | + s3 { |
| 62 | + bucket = module.s3_bucket.s3_bucket_id |
| 63 | + key = aws_s3_object.unsigned.id |
| 64 | + version = aws_s3_object.unsigned.version_id |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + destination { |
| 69 | + s3 { |
| 70 | + bucket = module.s3_bucket.s3_bucket_id |
| 71 | + prefix = "signed/" |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + ignore_signing_job_failure = true |
| 76 | +} |
| 77 | + |
| 78 | +resource "aws_lambda_code_signing_config" "this" { |
| 79 | + allowed_publishers { |
| 80 | + signing_profile_version_arns = [aws_signer_signing_profile.this.version_arn] |
| 81 | + } |
| 82 | + |
| 83 | + policies { |
| 84 | + untrusted_artifact_on_deployment = "Enforce" |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +################################################################################ |
| 89 | +# Supporting Resources |
| 90 | +################################################################################ |
| 91 | + |
| 92 | +resource "random_pet" "this" { |
| 93 | + length = 2 |
| 94 | +} |
| 95 | + |
| 96 | +module "s3_bucket" { |
| 97 | + source = "terraform-aws-modules/s3-bucket/aws" |
| 98 | + version = "~> 3.0" |
| 99 | + |
| 100 | + bucket_prefix = "${random_pet.this.id}-" |
| 101 | + force_destroy = true |
| 102 | + |
| 103 | + # S3 bucket-level Public Access Block configuration |
| 104 | + block_public_acls = true |
| 105 | + block_public_policy = true |
| 106 | + ignore_public_acls = true |
| 107 | + restrict_public_buckets = true |
| 108 | + |
| 109 | + versioning = { |
| 110 | + enabled = true |
| 111 | + } |
| 112 | + |
| 113 | + server_side_encryption_configuration = { |
| 114 | + rule = { |
| 115 | + apply_server_side_encryption_by_default = { |
| 116 | + sse_algorithm = "AES256" |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | +} |
0 commit comments