$npx -y skills add giuseppe-trisciuoglio/developer-kit --skill aws-cloudformation-cloudfrontProvides AWS CloudFormation patterns for CloudFront distributions, origins (ALB, S3, Lambda@Edge, VPC Origins), CacheBehaviors, Functions, SecurityHeaders, parameters, Outputs and cross-stack references. Use when creating CloudFront distributions with CloudFormation, configuring
| 1 | # AWS CloudFormation CloudFront CDN |
| 2 | |
| 3 | ## Overview |
| 4 | |
| 5 | Create production-ready CDN infrastructure using AWS CloudFormation templates. This skill covers CloudFront distributions, multiple origins (ALB, S3, API Gateway, Lambda@Edge, VPC Origins), CacheBehaviors, Functions, SecurityHeaders, and best practices for parameters, outputs, and cross-stack references. |
| 6 | |
| 7 | ## When to Use |
| 8 | |
| 9 | - Creating CloudFront distributions with CloudFormation |
| 10 | - Configuring origins (ALB, S3, Lambda@Edge, VPC Origins) with path patterns |
| 11 | - Implementing caching with CacheBehaviors and Cache Policies |
| 12 | - Configuring custom domains with ACM and SecurityHeaders |
| 13 | - Integrating WAF with CloudFront distributions |
| 14 | |
| 15 | ## Instructions |
| 16 | |
| 17 | Follow these steps to create CloudFront distributions with CloudFormation: |
| 18 | |
| 19 | ### 1. Define Distribution Parameters |
| 20 | |
| 21 | **Validate before deploying:** |
| 22 | ```bash |
| 23 | aws cloudformation validate-template --template-body file://template.yaml |
| 24 | cfn-lint template.yaml |
| 25 | ``` |
| 26 | |
| 27 | Specify domain names, ACM certificates, price class, and origin settings: |
| 28 | |
| 29 | ```yaml |
| 30 | Parameters: |
| 31 | DomainName: |
| 32 | Type: String |
| 33 | Default: cdn.example.com |
| 34 | Description: Custom domain name for CloudFront distribution |
| 35 | |
| 36 | CertificateArn: |
| 37 | Type: AWS::ACM::Certificate::Arn |
| 38 | Description: ACM certificate ARN for HTTPS |
| 39 | |
| 40 | PriceClass: |
| 41 | Type: String |
| 42 | Default: PriceClass_All |
| 43 | AllowedValues: |
| 44 | - PriceClass_All |
| 45 | - PriceClass_100 |
| 46 | - PriceClass_200 |
| 47 | Description: CloudFront price class |
| 48 | |
| 49 | OriginDomainName: |
| 50 | Type: String |
| 51 | Description: Domain name of the origin (ALB or S3) |
| 52 | ``` |
| 53 | |
| 54 | ### 2. Configure Origins |
| 55 | |
| 56 | Add S3 buckets, ALBs, API Gateway, or custom origins. For S3 origins, use OAI (legacy) or OAC (recommended): |
| 57 | |
| 58 | ```yaml |
| 59 | Resources: |
| 60 | # S3 Bucket |
| 61 | StaticBucket: |
| 62 | Type: AWS::S3::Bucket |
| 63 | Properties: |
| 64 | BucketName: !Sub "static-assets-${AWS::AccountId}-${AWS::Region}" |
| 65 | PublicAccessBlockConfiguration: |
| 66 | BlockPublicAcls: true |
| 67 | BlockPublicPolicy: true |
| 68 | |
| 69 | # Origin Access Control (recommended) |
| 70 | OriginAccessControl: |
| 71 | Type: AWS::CloudFront::OriginAccessControl |
| 72 | Properties: |
| 73 | OriginAccessControlConfig: |
| 74 | Name: !Sub "${AWS::StackName}-oac" |
| 75 | OriginAccessControlOriginType: s3 |
| 76 | SigningBehavior: always |
| 77 | SigningProtocol: sigv4 |
| 78 | ``` |
| 79 | |
| 80 | ### 3. Set Up Default Cache Behavior |
| 81 | |
| 82 | Configure viewer request/response policies and caching: |
| 83 | |
| 84 | ```yaml |
| 85 | Resources: |
| 86 | CloudFrontDistribution: |
| 87 | Type: AWS::CloudFront::Distribution |
| 88 | Properties: |
| 89 | DistributionConfig: |
| 90 | Origins: |
| 91 | - Id: S3Origin |
| 92 | DomainName: !GetAtt StaticBucket.RegionalDomainName |
| 93 | AccessControlId: !Ref OriginAccessControl |
| 94 | S3OriginConfig: |
| 95 | OriginAccessIdentity: "" |
| 96 | DefaultCacheBehavior: |
| 97 | TargetOriginId: S3Origin |
| 98 | ViewerProtocolPolicy: redirect-to-https |
| 99 | AllowedMethods: |
| 100 | - GET |
| 101 | - HEAD |
| 102 | CachedMethods: |
| 103 | - GET |
| 104 | - HEAD |
| 105 | Compress: true |
| 106 | CachePolicyId: !Ref CachePolicy |
| 107 | ``` |
| 108 | |
| 109 | ### 4. Add Additional Cache Behaviors |
| 110 | |
| 111 | Create path-specific caching rules for different content types: |
| 112 | |
| 113 | ```yaml |
| 114 | Resources: |
| 115 | ApiCachePolicy: |
| 116 | Type: AWS::CloudFront::CachePolicy |
| 117 | Properties: |
| 118 | CachePolicyConfig: |
| 119 | Name: !Sub "${AWS::StackName}-api-cache" |
| 120 | DefaultTTL: 300 |
| 121 | MaxTTL: 600 |
| 122 | MinTTL: 60 |
| 123 | |
| 124 | CloudFrontDistribution: |
| 125 | Type: AWS::CloudFront::Distribution |
| 126 | Properties: |
| 127 | DistributionConfig: |
| 128 | CacheBehaviors: |
| 129 | - PathPattern: "/api/*" |
| 130 | TargetOriginId: ApiOrigin |
| 131 | CachePolicyId: !GetAtt ApiCachePolicy.Id |
| 132 | AllowedMethods: |
| 133 | - GET |
| 134 | - HEAD |
| 135 | - OPTIONS |
| 136 | - PUT |
| 137 | - POST |
| 138 | ``` |
| 139 | |
| 140 | ### 5. Configure Security Settings |
| 141 | |
| 142 | Implement security headers and WAF integration: |
| 143 | |
| 144 | ```yaml |
| 145 | Resources: |
| 146 | SecurityHeadersPolicy: |
| 147 | Type: AWS::CloudFront::ResponseHeadersPolicy |
| 148 | Properties: |
| 149 | ResponseHeadersPolicyConfig: |
| 150 | Name: !Sub "${AWS::StackName}-security-headers" |
| 151 | SecurityHeadersConfig: |
| 152 | StrictTransportSecurity: |
| 153 | AccessControlMaxAgeSec: 31536000 |
| 154 | IncludeSubdomains: true |
| 155 | Override: true |
| 156 | FrameOptions: |
| 157 | FrameOption: DENY |
| 158 | Override: true |
| 159 | |
| 160 | WAFWebACL: |
| 161 | Type: AWS::WAFv2::WebACL |
| 162 | Properties: |
| 163 | Name: !Sub "${AWS::StackName}-waf" |
| 164 | Scope: CLOUDFRONT |
| 165 | DefaultAction: |
| 166 | Allow: {} |
| 167 | ``` |
| 168 | |
| 169 | ### 6. Add CloudFront Functions |