Skip to content

Commit 0700801

Browse files
committed
Updated example for VPC Origin
1 parent 739a2de commit 0700801

File tree

7 files changed

+34
-101
lines changed

7 files changed

+34
-101
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
repos:
22
- repo: https://github.com/antonbabenko/pre-commit-terraform
3-
rev: v1.96.1
3+
rev: v1.96.2
44
hooks:
55
- id: terraform_fmt
66
- id: terraform_wrapper_module_for_each

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ module "cdn" {
7979

8080
## Examples
8181

82-
- [Complete](https://github.com/terraform-aws-modules/terraform-aws-cloudfront/tree/master/examples/complete) - Complete example which creates AWS CloudFront distribution and integrates it with other [terraform-aws-modules](https://github.com/terraform-aws-modules) to create additional resources: S3 buckets, Lambda Functions, CloudFront Functions, ACM Certificate, Route53 Records.
82+
- [Complete](https://github.com/terraform-aws-modules/terraform-aws-cloudfront/tree/master/examples/complete) - Complete example which creates AWS CloudFront distribution and integrates it with other [terraform-aws-modules](https://github.com/terraform-aws-modules) to create additional resources: S3 buckets, Lambda Functions, CloudFront Functions, VPC Origins, ACM Certificate, Route53 Records.
8383

8484
## Notes
8585

@@ -107,13 +107,13 @@ ordered_cache_behavior = [{
107107
| Name | Version |
108108
|------|---------|
109109
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 0.13.1 |
110-
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 5.82.0 |
110+
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 5.82 |
111111

112112
## Providers
113113

114114
| Name | Version |
115115
|------|---------|
116-
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 5.82.0 |
116+
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 5.82 |
117117

118118
## Modules
119119

examples/complete/README.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Configuration in this directory creates CloudFront distribution which demos such
88
- Lambda@Edge
99
- ACM certificate
1010
- Route53 record
11+
- VPC Origins
1112

1213
## Usage
1314

@@ -50,9 +51,6 @@ Note that this example may create resources which cost money. Run `terraform des
5051
| <a name="module_log_bucket"></a> [log\_bucket](#module\_log\_bucket) | terraform-aws-modules/s3-bucket/aws | ~> 4.0 |
5152
| <a name="module_records"></a> [records](#module\_records) | terraform-aws-modules/route53/aws//modules/records | ~> 2.0 |
5253
| <a name="module_s3_one"></a> [s3\_one](#module\_s3\_one) | terraform-aws-modules/s3-bucket/aws | ~> 4.0 |
53-
| <a name="module_security_group_ec2"></a> [security\_group\_ec2](#module\_security\_group\_ec2) | terraform-aws-modules/security-group/aws | ~> 5.0 |
54-
| <a name="module_vpc"></a> [vpc](#module\_vpc) | terraform-aws-modules/vpc/aws | ~> 5.0 |
55-
| <a name="module_vpc_endpoints"></a> [vpc\_endpoints](#module\_vpc\_endpoints) | terraform-aws-modules/vpc/aws//modules/vpc-endpoints | ~> 5.0 |
5654

5755
## Resources
5856

@@ -63,12 +61,10 @@ Note that this example may create resources which cost money. Run `terraform des
6361
| [null_resource.download_package](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource |
6462
| [random_pet.this](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/pet) | resource |
6563
| [aws_ami.al2023](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/ami) | data source |
66-
| [aws_availability_zones.available](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/availability_zones) | data source |
6764
| [aws_canonical_user_id.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/canonical_user_id) | data source |
6865
| [aws_cloudfront_log_delivery_canonical_user_id.cloudfront](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/cloudfront_log_delivery_canonical_user_id) | data source |
6966
| [aws_iam_policy_document.s3_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
7067
| [aws_route53_zone.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/route53_zone) | data source |
71-
| [aws_security_group.vpc_origin](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/security_group) | data source |
7268

7369
## Inputs
7470

examples/complete/main.tf

Lines changed: 25 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ module "cloudfront" {
5656
create_vpc_origin = true
5757
vpc_origin = {
5858
ec2_vpc_origin = {
59-
name = local.subdomain
59+
name = random_pet.this.id
6060
arn = module.ec2.arn
6161
http_port = 80
6262
https_port = 443
@@ -348,6 +348,10 @@ module "records" {
348348
]
349349
}
350350

351+
#########################################
352+
# S3 bucket policy
353+
#########################################
354+
351355
data "aws_iam_policy_document" "s3_policy" {
352356
# Origin Access Identities
353357
statement {
@@ -383,108 +387,41 @@ resource "aws_s3_bucket_policy" "bucket_policy" {
383387
policy = data.aws_iam_policy_document.s3_policy.json
384388
}
385389

386-
########
387-
# Extra
388-
########
389-
390-
resource "random_pet" "this" {
391-
length = 2
392-
}
390+
#########################################
391+
# CloudFront function
392+
#########################################
393393

394394
resource "aws_cloudfront_function" "example" {
395395
name = "example-${random_pet.this.id}"
396396
runtime = "cloudfront-js-1.0"
397397
code = file("${path.module}/example-function.js")
398398
}
399399

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-
}
400+
#########################################
401+
# EC2 instance for CloudFront VPC origin
402+
#########################################
408403

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-
427-
module "vpc" {
428-
source = "terraform-aws-modules/vpc/aws"
429-
version = "~> 5.0"
430-
431-
name = local.subdomain
432-
cidr = local.vpc_cidr
433-
434-
azs = local.vpc_azs
435-
intra_subnets = [for k, v in local.vpc_azs : cidrsubnet(local.vpc_cidr, 8, k)]
436-
public_subnets = [for k, v in local.vpc_azs : cidrsubnet(local.vpc_cidr, 8, k + 4)]
437-
}
438-
439-
module "vpc_endpoints" {
440-
source = "terraform-aws-modules/vpc/aws//modules/vpc-endpoints"
441-
version = "~> 5.0"
442-
443-
vpc_id = module.vpc.vpc_id
404+
data "aws_ami" "al2023" {
405+
most_recent = true
406+
owners = ["amazon"]
444407

445-
endpoints = {
446-
s3 = {
447-
service = "s3"
448-
service_type = "Gateway"
449-
route_table_ids = module.vpc.intra_route_table_ids
450-
},
408+
filter {
409+
name = "name"
410+
values = ["al2023-ami-2023*-x86_64"]
451411
}
452412
}
453413

454-
module "security_group_ec2" {
455-
source = "terraform-aws-modules/security-group/aws"
414+
module "ec2" {
415+
source = "terraform-aws-modules/ec2-instance/aws"
456416
version = "~> 5.0"
457417

458-
name = "${local.subdomain}-ec2"
459-
description = "Security Group for EC2 Instance Egress"
460-
461-
vpc_id = module.vpc.vpc_id
462-
463-
egress_rules = ["http-80-tcp", "https-443-tcp"]
464-
ingress_with_source_security_group_id = [
465-
{
466-
from_port = 80
467-
to_port = 80
468-
protocol = "tcp"
469-
description = "Allow access to the CloudFront origin"
470-
source_security_group_id = data.aws_security_group.vpc_origin.id
471-
}]
418+
ami = data.aws_ami.al2023.id
472419
}
473420

474-
data "aws_availability_zones" "available" {}
475-
476-
data "aws_security_group" "vpc_origin" {
477-
name = "CloudFront-VPCOrigins-Service-SG"
478-
vpc_id = module.vpc.vpc_id
479-
depends_on = [module.cloudfront]
480-
}
481-
482-
data "aws_ami" "al2023" {
483-
most_recent = true
484-
owners = ["amazon"]
421+
########
422+
# Extra
423+
########
485424

486-
filter {
487-
name = "name"
488-
values = ["al2023-ami-2023*-x86_64"]
489-
}
425+
resource "random_pet" "this" {
426+
length = 2
490427
}

main.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ resource "aws_cloudfront_origin_access_control" "this" {
2828
resource "aws_cloudfront_vpc_origin" "this" {
2929
for_each = local.create_vpc_origin ? var.vpc_origin : {}
3030

31-
tags = var.tags
32-
3331
vpc_origin_endpoint_config {
3432
name = each.value["name"]
3533
arn = each.value["arn"]
@@ -42,6 +40,8 @@ resource "aws_cloudfront_vpc_origin" "this" {
4240
quantity = each.value.origin_ssl_protocols.quantity
4341
}
4442
}
43+
44+
tags = var.tags
4545
}
4646

4747
resource "aws_cloudfront_distribution" "this" {

versions.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ terraform {
44
required_providers {
55
aws = {
66
source = "hashicorp/aws"
7-
version = ">= 5.82.0"
7+
version = ">= 5.82"
88
}
99
}
1010
}

wrappers/versions.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ terraform {
44
required_providers {
55
aws = {
66
source = "hashicorp/aws"
7-
version = ">= 5.82.0"
7+
version = ">= 5.82"
88
}
99
}
1010
}

0 commit comments

Comments
 (0)