Skip to content

giuseppe-trisciuoglio/developer-kit / plugins/developer-kit-aws/skills/aws-cloudformation/aws-cloudformation-cloudfront

aws-cloudformation-cloudfront

Verified

Provides 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 multiple origins, implementing caching strategies, managing custom domains with ACM, configuring WAF, and optimizing performance.

FileKindSize
SKILL.mdskill md14.1 KB
references/advanced-features.mdmarkdown14.0 KB
references/caching.mdmarkdown13.7 KB
references/constraints.mdmarkdown12.4 KB
references/examples.mdmarkdown41.5 KB
references/origins.mdmarkdown17.0 KB
references/reference.mdmarkdown25.2 KB
references/security.mdmarkdown13.5 KB
references/template-structure.mdmarkdown11.5 KB

references/security.md

View on GitHub
# CloudFront Security Configuration

## Security Headers

### Complete Security Headers Policy

```yaml
Resources:
  SecurityHeadersPolicy:
    Type: AWS::CloudFront::ResponseHeadersPolicy
    Properties:
      ResponseHeadersPolicyConfig:
        Name: !Sub "${AWS::StackName}-security-headers"
        SecurityHeadersConfig:
          ContentTypeOptions:
            Override: true
          FrameOptions:
            FrameOption: DENY
            Override: true
          ReferrerPolicy:
            ReferrerPolicy: strict-origin-when-cross-origin
            Override: true
          StrictTransportSecurity:
            AccessControlMaxAgeSec: 31536000
            IncludeSubdomains: true
            Override: true
            Preload: true
          XSSProtection:
            ModeBlock: true
            Override: true
            Protection: true
```

### CORS Configuration

```yaml
Resources:
  CorsHeadersPolicy:
    Type: AWS::CloudFront::ResponseHeadersPolicy
    Properties:
      ResponseHeadersPolicyConfig:
        Name: !Sub "${AWS::StackName}-cors-headers"
        CorsConfig:
          AccessControlAllowCredentials: false
          AccessControlAllowHeaders:
            Items:
              - "*"
          AccessControlAllowMethods:
            Items:
              - GET
              - HEAD
              - OPTIONS
              - POST
              - PUT
              - DELETE
          AccessControlAllowOrigins:
            Items:
              - https://example.com
              - https://www.example.com
          AccessControlExposeHeaders:
            Items:
              - Content-Length
              - Content-Type
              - X-Custom-Header
          AccessControlMaxAgeSec: 600
          OriginOverride: true
```

### Custom Security Headers

```yaml
Resources:
  CustomSecurityPolicy:
    Type: AWS::CloudFront::ResponseHeadersPolicy
    Properties:
      ResponseHeadersPolicyConfig:
        Name: !Sub "${AWS::StackName}-custom-security"
        CustomHeadersConfig:
          Items:
            - Header: X-Frame-Options
              Value: DENY
              Override: true
            - Header: X-Content-Type-Options
              Value: nosniff
              Override: true
            - Header: X-XSS-Protection
              Value: "1; mode=block"
              Override: true
            - Header: Permissions-Policy
              Value: "geolocation=(), microphone=(), camera=()"
              Override: true
```

## WAF Integration

### WAF Web ACL with Managed Rules

```yaml
Resources:
  CloudFrontWebACL:
    Type: AWS::WAFv2::WebACL
    Properties:
      Name: !Sub "${AWS::StackName}-waf-acl"
      Scope: CLOUDFRONT
      DefaultAction:
        Allow: {}
      Rules:
        # AWS Managed Rule - Common
        - Name: AWSCommonRule
          Priority: 1
          Statement:
            ManagedRuleGroupStatement:
              VendorName: AWS
              Name: AWSManagedRulesCommonRuleSet
              ExcludedRules:
                - Name: SizeRestrictions_BODY
          OverrideAction:
            None: {}
          VisibilityConfig:
            SampledRequestsEnabled: true
            CloudWatchMetricsEnabled: true
            MetricName: AWSCommonRule

        # Rate-based rule
        - Name: RateLimitRule
          Priority: 2
          Statement:
            RateBasedStatementKey:
              SingleHeader:
                Name: ip
              AggregateKeyType: IP
              Limit: 1000
          OverrideAction:
            None: {}
          VisibilityConfig:
            SampledRequestsEnabled: true
            CloudWatchMetricsEnabled: true
            MetricName: RateLimitRule

        # SQL Injection protection
        - Name: SQLInjectionRule
          Priority: 3
          Statement:
            SqliMatchStatement:
              FieldToMatch:
                QueryString: {}
                UriPath: {}
              TextTransformations:
                - Priority: 1
                  Type: URL_DECODE
                - Priority: 2
                  Type: LOWERCASE
          OverrideAction:
            None: {}
          VisibilityConfig:
            SampledRequestsEnabled: true
            CloudWatchMetricsEnabled: true
            MetricName: SQLInjectionRule

        # XSS protection
        - Name: XSSRule
          Priority: 4
          Statement:
            XssMatchStatement:
              FieldToMatch:
                QueryString: {}
                UriPath: {}
              TextTransformations:
                - Priority: 1
                  Type: URL_DECODE
                - Priority: 2
                  Type: LOWERCASE
          OverrideAction:
            None: {}
          VisibilityConfig:
            SampledRequestsEnabled: true
            CloudWatchMetricsEnabled: true
            MetricName: XSSRule

      VisibilityConfig:
        SampledRequestsEnabled: true
        CloudWatchMetricsEnabled: true
        MetricName: CloudFrontWAFACL

  CloudFrontDistribution:
    Type: AWS::CloudFront::Distribution
    Properties:
      DistributionConfig:
        WebACLId: !GetAtt CloudFrontWebACL.Arn
```

### WAF with Custom Rules

```yaml
Resources:
  CustomWAFWebACL:
    Type: AWS::WAFv2::WebACL
    Properties:
      Name: !Sub "${AWS::StackName}-custom-waf"
      Scope: CLOUDFRONT
      DefaultAction:
        Block: {}
      Rules:
        # Block specific user agents
        - Name: BlockBadBots
          Priority: 1
          Statement:
            ByteMatchStatement:
              FieldToMatch:
                HeaderName: User-Agent
              SearchString: "BadBot"
              PositionalConstraint: CONTAINS
          Action:
            Block: {}
          VisibilityConfig:
            SampledRequestsEnabled: true
            CloudWatchMetricsEnabled: true
            MetricName: BlockBadBots

        # Rate limit per IP
        - Name: PerIPRateLimit
          Priority: 2
          Statement:
            RateBasedStatementKey:
              ForwardedIP:
                AggregateKeyType: IP
                Limit: 500
          Action:
            Block: {}
          VisibilityConfig:
            SampledRequestsEnabled: true
            CloudWatchMetricsEnabled: true
            MetricName: PerIPRateLimit

        # Allow specific IP ranges
        - Name: AllowOfficeIPs
          Priority: 3
          Statement:
            IpSetReferenceStatement:
              Arn: !GetAtt OfficeIPSet.IPSetArn
          Action:
            Allow: {}
          VisibilityConfig:
            SampledRequestsEnabled: true
            CloudWatchMetricsEnabled: true
            MetricName: AllowOfficeIPs

  OfficeIPSet:
    Type: AWS::WAFv2::IPSet
    Properties:
      Name: office-ips
      Scope: CLOUDFRONT
      Addresses:
        - Address: 192.0.2.0/24
        - Address: 203.0.113.0/24
```

## Origin Access Control

### OAI (Legacy)

```yaml
Resources:
  # S3 Bucket
  StaticBucket:
    Type: AWS::S3::Bucket
    Properties:
      BucketName: !Sub "static-assets-${AWS::AccountId}-${AWS::Region}"
      PublicAccessBlockConfiguration:
        BlockPublicAcls: true
        BlockPublicPolicy: true
        IgnorePublicAcls: true
        RestrictPublicBuckets: true

  # CloudFront OAI
  CloudFrontOAI:
    Type: AWS::CloudFront::CloudFrontOriginAccessIdentity
    Properties:
      CloudFrontOriginAccessIdentityConfig:
        Comment: !Sub "OAI for ${StaticBucket}"

  # S3 Bucket Policy - Allow CloudFront OAI
  S3BucketPolicy:
    Type: AWS::S3::BucketPolicy
    Properties:
      Bucket: !Ref StaticBucket
      PolicyDocument:
        Version: "2012-10-17"
        Statement:
          - Effect: Allow
            Principal:
              CanonicalUser: !GetAtt CloudFrontOAI.S3CanonicalUserId
            Action: s3:GetObject
            Resource: !Sub "${StaticBucket.Arn}/*"
          - Effect: Deny
            Principal: "*"
            Action: s3:GetObject
            Resource: !Sub "${StaticBucket.Arn}/*"
            Condition:
              Bool:
                aws:SecureTransport: false

  CloudFrontDistribution:
    Type: AWS::CloudFront::Distribution
    Properties:
      DistributionConfig:
        Origins:
          - Id: S3Origin
            DomainName: !GetAtt StaticBucket.RegionalDomainName
            S3OriginConfig:
              OriginAccessIdentity: !Sub "origin-access-identity/cloudfront/${CloudFrontOAI}"
```

### OAC (Origin Access Control - Recommended)

```yaml
Resources:
  StaticBucket:
    Type: AWS::S3::Bucket
    Properties:
      BucketName: !Sub "static-assets-oac-${AWS::AccountId}-${AWS::Region}"
      OwnershipControls:
        Rules:
          - ObjectOwnership: BucketOwnerPreferred
      PublicAccessBlockConfiguration:
        BlockPublicAcls: true
        BlockPublicPolicy: true
        IgnorePublicAcls: true
        RestrictPublicBuckets: true

  # S3 Bucket Policy for OAC
  S3BucketPolicy:
    Type: AWS::S3::BucketPolicy
    Properties:
      Bucket: !Ref StaticBucket
      PolicyDocument:
        Version: "2012-10-17"
        Statement:
          - Effect: Allow
            Principal:
              Service: cloudfront.amazonaws.com
            Action: s3:GetObject
            Resource: !Sub "${StaticBucket.Arn}/*"
            Condition:
              StringEquals:
                AWS:SourceArn: !Sub "arn:aws:cloudfront::${AWS::AccountId}:distribution/${CloudFrontDistribution}"

  CloudFrontDistribution:
    Type: AWS::CloudFront::Distribution
    Properties:
      DistributionConfig:
        Origins:
          - Id: S3Origin
            DomainName: !GetAtt StaticBucket.RegionalDomainName
            AccessControlId: !Ref OriginAccessControl
            S3OriginConfig:
              OriginAccessIdentity: ""
```

### Origin Access Control with OAC

```yaml
Resources:
  OriginAccessControl:
    Type: AWS::CloudFront::OriginAccessControl
    Properties:
      OriginAccessControlConfig:
        Name: !Sub "${AWS::StackName}-oac"
        SigningBehavior: no-origin-access-control
        OriginAccessControlSignatures:
          - # Origin access control not required for this origin
```

## Signed URLs and Signed Cookies

### Key Pair for Signed URLs

```yaml
Resources:
  # CloudFront Key Pair
  CloudFrontKeyPair:
    Type: AWS::CloudFront::PublicKey
    Properties:
      PublicKeyConfig:
        CallerReference: !Sub "${AWS::StackName}-keypair"
        EncodedKey: !Ref PublicKeyEncoded

  # Store encoded key in Parameter Store
  EncodedKeyParameter:
    Type: AWS::SSM::Parameter
    Properties:
      Name: /cloudfront/public-key-encoded
      Type: String
      Value: !Ref PublicKeyEncoded
```

### Trusted Signers

```yaml
Resources:
  TrustedSigners:
    Type: AWS::CloudFront::CloudFrontOriginAccessIdentity
    Properties:
      CloudFrontOriginAccessIdentityConfig:
        Comment: Trusted signers for signed URLs
```

## Geo-Restrictions

### Whitelist Countries

```yaml
Resources:
  CloudFrontDistribution:
    Type: AWS::CloudFront::Distribution
    Properties:
      DistributionConfig:
        GeoRestriction:
          RestrictionType: whitelist
          Locations:
            - US
            - CA
            - GB
            - DE
            - FR
            - IT
            - JP
            - AU
```

### Blacklist Countries

```yaml
Resources:
  CloudFrontDistribution:
    Type: AWS::CloudFront::Distribution
    Properties:
      DistributionConfig:
        GeoRestriction:
          RestrictionType: none
          # To blacklist, use restrictionType: blacklist
          # Locations:
          #   - CN
          #   - RU
```

## HTTPS Only

### Enforce HTTPS

```yaml
Resources:
  CloudFrontDistribution:
    Type: AWS::CloudFront::Distribution
    Properties:
      DistributionConfig:
        DefaultCacheBehavior:
          ViewerProtocolPolicy: redirect-to-https
        CacheBehaviors:
          - PathPattern: "/api/*"
            ViewerProtocolPolicy: https-only
```

### TLS Configuration

```yaml
Resources:
  CloudFrontDistribution:
    Type: AWS::CloudFront::Distribution
    Properties:
      DistributionConfig:
        ViewerCertificate:
          AcmCertificateArn: !Ref CertificateArn
          MinimumProtocolVersion: TLSv1.2_2021
          SslSupportMethod: sni-only
```

## Field-Level Encryption

```yaml
Resources:
  # Encryption configuration
  FieldLevelEncryptionConfig:
    Type: AWS::CloudFront::FieldLevelEncryptionConfig
    Properties:
      Comment: Encrypt sensitive fields
      ContentTypeProfileConfig:
        ContentTypeProfiles:
          - ContentType: application/x-www-form-urlencoded
            Format: JSON
          - ContentType: application/json
            Format: JSON
      FieldLevelEncryptionProfileConfig:
        FieldLevelEncryptionProfile:
          Name: encrypt-fields
          EncryptionEntities:
            FieldPatterns:
              Items:
                - FieldType: QUERY_STRING
                  Value: creditcard
            FieldPatterns:
              Items:
                - FieldType: BODY
                  Value: ssn
          KMSKeyArn: !Ref EncryptionKey
```

## Security Best Practices

### HTTPS Enforcement

```yaml
ViewerCertificate:
  AcmCertificateArn: !Ref CertificateArn
  MinimumProtocolVersion: TLSv1.2_2021
  SslSupportMethod: sni-only
```

### Content Security Policy

```yaml
- Header: Content-Security-Policy
  Value: "default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; img-src 'self' data: https:; font-src 'self'; connect-src 'self'; frame-ancestors 'none';"
  Override: true
```

### Prevent Clickjacking

```yaml
- Header: X-Frame-Options
  Value: DENY
  Override: true
```

### Prevent MIME Sniffing

```yaml
- Header: X-Content-Type-Options
  Value: nosniff
  Override: true
```
rea-analysis-optimizationmorluto/reaOptimize REA providers and shared analysis workflows for agent context, latency, CPU, and memory while preserving correctness and Evidence. Use for analysis bottlenecks and cache or query design; CI/test-suite optimization belongs to rea-ci-tests-audit.Stars: 470kUpdated Claude CodeCodexVerifiedInstall
rea-ci-tests-auditmorluto/reaAudit REA CI and test suites for correctness, fixture drift, false passes, and measured performance; use for suite or workflow investigations and efficiency changes, rather than ordinary feature testing or a single test failure.Stars: 470kUpdated Claude CodeCodexVerifiedInstall
rea-tool-designmorluto/reaAudit, investigate, design, or change REA investigation tools, CLI/MCP contracts, provider capabilities, and Evidence semantics.Stars: 470kUpdated Claude CodeCodexVerifiedInstall
ponytail-auditDietrichGebert/ponytailQuality audit of the whole repo: bugs, security, real load, missing tests, speed, and what to delete. Most important first.Stars: 194kUpdated Claude CodeCodexVerifiedInstall