|
| 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 | +data "aws_region" "current" {} |
| 13 | + |
| 14 | +################################################################################ |
| 15 | +# Lambda Module |
| 16 | +################################################################################ |
| 17 | + |
| 18 | +module "lambda_s3_write" { |
| 19 | + source = "../../" |
| 20 | + |
| 21 | + description = "Lambda demonstrating writes to an S3 bucket from within a VPC without Internet access" |
| 22 | + |
| 23 | + function_name = random_pet.this.id |
| 24 | + handler = "index.lambda_handler" |
| 25 | + runtime = "python3.8" |
| 26 | + |
| 27 | + source_path = "${path.module}/../fixtures/python3.8-app2" |
| 28 | + |
| 29 | + environment_variables = { |
| 30 | + BUCKET_NAME = module.s3_bucket.s3_bucket_id |
| 31 | + REGION_NAME = data.aws_region.current.name |
| 32 | + } |
| 33 | + |
| 34 | + # Let the module create a role for us |
| 35 | + create_role = true |
| 36 | + attach_cloudwatch_logs_policy = true |
| 37 | + attach_network_policy = true |
| 38 | + |
| 39 | + # There's no need to attach any extra permission for S3 writes as that's added by the bucket policy when a session is created |
| 40 | + # See https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies.html |
| 41 | + |
| 42 | + vpc_security_group_ids = [module.security_group_lambda.security_group_id] |
| 43 | + vpc_subnet_ids = module.vpc.intra_subnets |
| 44 | + |
| 45 | + tags = { |
| 46 | + Module = "lambda_s3_write" |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +################################################################################ |
| 51 | +# Extra Resources |
| 52 | +################################################################################ |
| 53 | + |
| 54 | +resource "random_pet" "this" { |
| 55 | + length = 2 |
| 56 | +} |
| 57 | + |
| 58 | +module "vpc" { |
| 59 | + source = "terraform-aws-modules/vpc/aws" |
| 60 | + version = "~> 3.0" |
| 61 | + |
| 62 | + name = random_pet.this.id |
| 63 | + cidr = "10.0.0.0/16" |
| 64 | + |
| 65 | + azs = ["${data.aws_region.current.name}a", "${data.aws_region.current.name}b", "${data.aws_region.current.name}c"] |
| 66 | + |
| 67 | + # Intra subnets are designed to have no Internet access via NAT Gateway. |
| 68 | + intra_subnets = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"] |
| 69 | +} |
| 70 | + |
| 71 | +module "vpc_endpoints" { |
| 72 | + source = "terraform-aws-modules/vpc/aws//modules/vpc-endpoints" |
| 73 | + version = "~> 3.0" |
| 74 | + |
| 75 | + vpc_id = module.vpc.vpc_id |
| 76 | + |
| 77 | + endpoints = { |
| 78 | + s3 = { |
| 79 | + service = "s3" |
| 80 | + service_type = "Gateway" |
| 81 | + route_table_ids = module.vpc.intra_route_table_ids |
| 82 | + policy = data.aws_iam_policy_document.endpoint.json |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +data "aws_iam_policy_document" "endpoint" { |
| 88 | + statement { |
| 89 | + sid = "RestrictBucketAccessToIAMRole" |
| 90 | + |
| 91 | + principals { |
| 92 | + type = "AWS" |
| 93 | + identifiers = ["*"] |
| 94 | + } |
| 95 | + |
| 96 | + actions = [ |
| 97 | + "s3:PutObject", |
| 98 | + ] |
| 99 | + |
| 100 | + resources = [ |
| 101 | + "${module.s3_bucket.s3_bucket_arn}/*", |
| 102 | + ] |
| 103 | + |
| 104 | + # See https://docs.aws.amazon.com/vpc/latest/privatelink/vpc-endpoints-s3.html#edit-vpc-endpoint-policy-s3 |
| 105 | + condition { |
| 106 | + test = "ArnEquals" |
| 107 | + variable = "aws:PrincipalArn" |
| 108 | + values = [module.lambda_s3_write.lambda_role_arn] |
| 109 | + } |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +module "kms" { |
| 114 | + source = "terraform-aws-modules/kms/aws" |
| 115 | + version = "~> 1.0" |
| 116 | + |
| 117 | + description = "S3 encryption key" |
| 118 | + |
| 119 | + # Grants |
| 120 | + grants = { |
| 121 | + lambda = { |
| 122 | + grantee_principal = module.lambda_s3_write.lambda_role_arn |
| 123 | + operations = [ |
| 124 | + "GenerateDataKey", |
| 125 | + ] |
| 126 | + } |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +module "s3_bucket" { |
| 131 | + source = "terraform-aws-modules/s3-bucket/aws" |
| 132 | + version = "~> 3.0" |
| 133 | + |
| 134 | + bucket_prefix = "${random_pet.this.id}-" |
| 135 | + force_destroy = true |
| 136 | + |
| 137 | + # S3 bucket-level Public Access Block configuration |
| 138 | + block_public_acls = true |
| 139 | + block_public_policy = true |
| 140 | + ignore_public_acls = true |
| 141 | + restrict_public_buckets = true |
| 142 | + |
| 143 | + versioning = { |
| 144 | + enabled = true |
| 145 | + } |
| 146 | + |
| 147 | + # Bucket policy |
| 148 | + attach_policy = true |
| 149 | + policy = data.aws_iam_policy_document.bucket.json |
| 150 | + |
| 151 | + server_side_encryption_configuration = { |
| 152 | + rule = { |
| 153 | + apply_server_side_encryption_by_default = { |
| 154 | + kms_master_key_id = module.kms.key_id |
| 155 | + sse_algorithm = "aws:kms" |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +data "aws_iam_policy_document" "bucket" { |
| 162 | + statement { |
| 163 | + sid = "RestrictBucketAccessToIAMRole" |
| 164 | + |
| 165 | + principals { |
| 166 | + type = "AWS" |
| 167 | + identifiers = [module.lambda_s3_write.lambda_role_arn] |
| 168 | + } |
| 169 | + |
| 170 | + actions = [ |
| 171 | + "s3:PutObject", |
| 172 | + ] |
| 173 | + |
| 174 | + resources = [ |
| 175 | + "${module.s3_bucket.s3_bucket_arn}/*", |
| 176 | + ] |
| 177 | + } |
| 178 | +} |
| 179 | + |
| 180 | +module "security_group_lambda" { |
| 181 | + source = "terraform-aws-modules/security-group/aws" |
| 182 | + version = "~> 4.0" |
| 183 | + |
| 184 | + name = random_pet.this.id |
| 185 | + description = "Security Group for Lambda Egress" |
| 186 | + |
| 187 | + vpc_id = module.vpc.vpc_id |
| 188 | + |
| 189 | + egress_cidr_blocks = [] |
| 190 | + egress_ipv6_cidr_blocks = [] |
| 191 | + |
| 192 | + # Prefix list ids to use in all egress rules in this module |
| 193 | + egress_prefix_list_ids = [module.vpc_endpoints.endpoints["s3"]["prefix_list_id"]] |
| 194 | + |
| 195 | + egress_rules = ["https-443-tcp"] |
| 196 | +} |
0 commit comments