@@ -53,6 +53,21 @@ module "cloudfront" {
5353 }
5454 }
5555
56+ create_vpc_origin = true
57+ vpc_origin = {
58+ ec2_vpc_origin = {
59+ name = local.subdomain
60+ arn = module.ec2.arn
61+ http_port = 80
62+ https_port = 443
63+ origin_protocol_policy = " http-only"
64+ origin_ssl_protocols = {
65+ items = [" TLSv1.2" ]
66+ quantity = 1
67+ }
68+ }
69+ }
70+
5671 logging_config = {
5772 bucket = module.log_bucket.s3_bucket_bucket_domain_name
5873 prefix = " cloudfront"
@@ -98,6 +113,14 @@ module "cloudfront" {
98113 origin_access_control = " s3_oac" # key in `origin_access_control`
99114 # origin_access_control_id = "E345SXM82MIOSU" # external OAС resource
100115 }
116+
117+ ec2_vpc_origin = {
118+ domain_name = module.ec2.private_dns
119+ vpc_origin_config = {
120+ vpc_origin = " ec2_vpc_origin" # key in `vpc_origin`
121+ # vpc_origin_id = "vo_Cg6A14otX0DB1yyDQ6Nond" # external VPC Origin resource
122+ }
123+ }
101124 }
102125
103126 origin_group = {
@@ -170,7 +193,16 @@ module "cloudfront" {
170193 # Using Cache/ResponseHeaders/OriginRequest policies is not allowed together with `compress` and `query_string` settings
171194 compress = true
172195 query_string = true
196+ },
197+ {
198+ path_pattern = " /vpc-origin/*"
199+ target_origin_id = " ec2_vpc_origin"
200+ viewer_protocol_policy = " redirect-to-https"
201+
202+ allowed_methods = [" GET" , " HEAD" , " OPTIONS" ]
203+ cached_methods = [" GET" , " HEAD" ]
173204 }
205+
174206 ]
175207
176208 viewer_certificate = {
@@ -364,3 +396,99 @@ resource "aws_cloudfront_function" "example" {
364396 runtime = " cloudfront-js-1.0"
365397 code = file (" ${ path . module } /example-function.js" )
366398}
399+
400+ # ######################################
401+ # EC2 and VPC for CloudFront VPC origin
402+ # ######################################
403+
404+ locals {
405+ vpc_cidr = " 10.0.0.0/16"
406+ vpc_azs = slice (data. aws_availability_zones . available . names , 0 , 2 )
407+ }
408+
409+ module "ec2" {
410+ source = " terraform-aws-modules/ec2-instance/aws"
411+ version = " ~> 5.0"
412+
413+ name = local. subdomain
414+ ami = data. aws_ami . al2023 . id
415+
416+ user_data = <<- EOF
417+ #!/bin/bash
418+ dnf update
419+ dnf install -y nginx
420+ systemctl start nginx
421+ EOF
422+
423+ subnet_id = element (module. vpc . intra_subnets , 0 )
424+ vpc_security_group_ids = [module . security_group_ec2 . security_group_id ]
425+
426+ create_iam_instance_profile = true
427+ iam_role_description = " IAM role for EC2 instance"
428+ iam_role_policies = {
429+ AmazonSSMManagedInstanceCore = " arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore"
430+ }
431+ }
432+ module "vpc" {
433+ source = " terraform-aws-modules/vpc/aws"
434+ version = " ~> 5.0"
435+
436+ name = local. subdomain
437+ cidr = local. vpc_cidr
438+
439+ azs = local. vpc_azs
440+ intra_subnets = [for k , v in local . vpc_azs : cidrsubnet (local. vpc_cidr , 8 , k)]
441+ public_subnets = [for k , v in local . vpc_azs : cidrsubnet (local. vpc_cidr , 8 , k + 4 )]
442+ }
443+
444+ module "vpc_endpoints" {
445+ source = " terraform-aws-modules/vpc/aws//modules/vpc-endpoints"
446+ version = " ~> 5.0"
447+
448+ vpc_id = module. vpc . vpc_id
449+
450+ endpoints = {
451+ s3 = {
452+ service = " s3"
453+ service_type = " Gateway"
454+ route_table_ids = module.vpc.intra_route_table_ids
455+ },
456+ }
457+ }
458+
459+ module "security_group_ec2" {
460+ source = " terraform-aws-modules/security-group/aws"
461+ version = " ~> 5.0"
462+
463+ name = " ${ local . subdomain } -ec2"
464+ description = " Security Group for EC2 Instance Egress"
465+
466+ vpc_id = module. vpc . vpc_id
467+
468+ egress_rules = [" http-80-tcp" , " https-443-tcp" ]
469+ ingress_with_source_security_group_id = [
470+ {
471+ from_port = 80
472+ to_port = 80
473+ protocol = " tcp"
474+ description = " Allow access to the CloudFront origin"
475+ source_security_group_id = data.aws_security_group.vpc_origin.id
476+ }]
477+ }
478+
479+ data "aws_availability_zones" "available" {}
480+
481+ data "aws_security_group" "vpc_origin" {
482+ name = " CloudFront-VPCOrigins-Service-SG"
483+ vpc_id = module. vpc . vpc_id
484+ }
485+
486+ data "aws_ami" "al2023" {
487+ most_recent = true
488+ owners = [" amazon" ]
489+
490+ filter {
491+ name = " name"
492+ values = [" al2023-ami-2023*-x86_64" ]
493+ }
494+ }
0 commit comments