$npx -y skills add giuseppe-trisciuoglio/developer-kit --skill aws-cloudformation-s3Provides AWS CloudFormation patterns for Amazon S3. Use when creating S3 buckets, policies, versioning, lifecycle rules, and implementing template structure with Parameters, Outputs, Mappings, Conditions, and cross-stack references.
| 1 | # AWS CloudFormation S3 Patterns |
| 2 | |
| 3 | Provides S3 bucket configurations, policies, versioning, lifecycle rules, and CloudFormation template structure best practices for production-ready infrastructure. |
| 4 | |
| 5 | ## When to Use |
| 6 | |
| 7 | - Creating S3 buckets with custom configurations |
| 8 | - Implementing bucket policies for access control |
| 9 | - Configuring S3 versioning for data protection |
| 10 | - Setting up lifecycle rules for data management |
| 11 | - Creating Outputs for cross-stack references |
| 12 | - Using Parameters with AWS-specific types |
| 13 | - Organizing templates with Mappings and Conditions |
| 14 | |
| 15 | ## Overview |
| 16 | |
| 17 | S3 bucket configurations, policies, versioning, lifecycle rules, and CloudFormation template structure for production-ready infrastructure. |
| 18 | |
| 19 | ## Instructions |
| 20 | |
| 21 | 1. **Define Bucket Resources**: Create `AWS::S3::Bucket` with versioning, encryption, PublicAccessBlock |
| 22 | 2. **Configure Bucket Policy**: Set up IAM policies for access control |
| 23 | 3. **Set Up Lifecycle Rules**: Define transitions and expiration policies |
| 24 | 4. **Configure CORS**: Allow cross-origin requests if needed |
| 25 | 5. **Add Outputs**: Export bucket names/ARNs for cross-stack references |
| 26 | |
| 27 | **Validate before deploy:** |
| 28 | ```bash |
| 29 | aws cloudformation validate-template --template-body file://template.yaml |
| 30 | ``` |
| 31 | |
| 32 | **Deploy with rollback on failure:** |
| 33 | ```bash |
| 34 | aws cloudformation deploy \ |
| 35 | --template-file template.yaml \ |
| 36 | --stack-name my-s3-stack \ |
| 37 | --capabilities CAPABILITY_IAM |
| 38 | ``` |
| 39 | |
| 40 | If deployment fails, CloudFormation automatically rolls back. Check failures with: |
| 41 | ```bash |
| 42 | aws cloudformation describe-stack-events --stack-name my-s3-stack |
| 43 | ``` |
| 44 | |
| 45 | ## Quick Reference |
| 46 | |
| 47 | | Resource Type | Purpose | |
| 48 | |---------------|---------| |
| 49 | | `AWS::S3::Bucket` | Create S3 bucket | |
| 50 | | `AWS::S3::BucketPolicy` | Set bucket-level policies | |
| 51 | | `AWS::S3::BucketReplication` | Cross-region replication | |
| 52 | | Parameters | Input values for customization | |
| 53 | | Mappings | Static configuration tables | |
| 54 | | Conditions | Conditional resource creation | |
| 55 | | Outputs | Return values for cross-stack references | |
| 56 | |
| 57 | ## Examples |
| 58 | |
| 59 | ### Basic S3 Bucket |
| 60 | |
| 61 | ```yaml |
| 62 | Resources: |
| 63 | DataBucket: |
| 64 | Type: AWS::S3::Bucket |
| 65 | Properties: |
| 66 | BucketName: my-data-bucket |
| 67 | ``` |
| 68 | |
| 69 | ### Bucket with Versioning and Encryption |
| 70 | |
| 71 | ```yaml |
| 72 | DataBucket: |
| 73 | Type: AWS::S3::Bucket |
| 74 | Properties: |
| 75 | BucketName: !Sub "${AWS::StackName}-data" |
| 76 | VersioningConfiguration: |
| 77 | Status: Enabled |
| 78 | BucketEncryption: |
| 79 | ServerSideEncryptionConfiguration: |
| 80 | - ServerSideEncryptionByDefault: |
| 81 | SSEAlgorithm: AES256 |
| 82 | PublicAccessBlockConfiguration: |
| 83 | BlockPublicAcls: true |
| 84 | BlockPublicPolicy: true |
| 85 | ``` |
| 86 | |
| 87 | ### Lifecycle Rule |
| 88 | |
| 89 | ```yaml |
| 90 | DataBucket: |
| 91 | Type: AWS::S3::Bucket |
| 92 | Properties: |
| 93 | LifecycleConfiguration: |
| 94 | Rules: |
| 95 | - Id: ArchiveOldData |
| 96 | Status: Enabled |
| 97 | Transitions: |
| 98 | - StorageClass: GLACIER |
| 99 | TransitionInDays: 365 |
| 100 | ``` |
| 101 | |
| 102 | ### Bucket Policy |
| 103 | |
| 104 | ```yaml |
| 105 | BucketPolicy: |
| 106 | Type: AWS::S3::BucketPolicy |
| 107 | Properties: |
| 108 | Bucket: !Ref DataBucket |
| 109 | PolicyDocument: |
| 110 | Statement: |
| 111 | - Effect: Allow |
| 112 | Principal: |
| 113 | AWS: !Ref RoleArn |
| 114 | Action: |
| 115 | - s3:GetObject |
| 116 | Resource: !Sub "${DataBucket.Arn}/*" |
| 117 | ``` |
| 118 | |
| 119 | See [references/complete-examples.md](references/complete-examples.md) for more complete examples including CORS, static websites, replication, and production-ready configurations. |
| 120 | |
| 121 | ## Template Structure |
| 122 | |
| 123 | ### Template Sections |
| 124 | |
| 125 | ```yaml |
| 126 | AWSTemplateFormatVersion: 2010-09-09 |
| 127 | Description: Template description |
| 128 | |
| 129 | Mappings: {} # Static configuration tables |
| 130 | Metadata: {} # Additional information |
| 131 | Parameters: {} # Input values |
| 132 | Conditions: {} # Conditional creation |
| 133 | Transform: {} # Macro processing |
| 134 | Resources: {} # AWS resources (REQUIRED) |
| 135 | Outputs: {} # Return values |
| 136 | ``` |
| 137 | |
| 138 | ### Parameters |
| 139 | |
| 140 | ```yaml |
| 141 | Parameters: |
| 142 | BucketName: |
| 143 | Type: String |
| 144 | Description: S3 bucket name |
| 145 | Default: my-bucket |
| 146 | MinLength: 3 |
| 147 | MaxLength: 63 |
| 148 | AllowedPattern: '^[a-z0-9-]+$' |
| 149 | ``` |
| 150 | |
| 151 | ### Conditions |
| 152 | |
| 153 | ```yaml |
| 154 | Conditions: |
| 155 | IsProduction: !Equals [!Ref Environment, prod] |
| 156 | ShouldEnableVersioning: !Equals [!Ref EnableVersioning, 'true'] |
| 157 | |
| 158 | Resources: |
| 159 | DataBucket: |
| 160 | Type: AWS::S3::Bucket |
| 161 | Properties: |
| 162 | VersioningConfiguration: |
| 163 | Status: !If [ShouldEnableVersioning, Enabled, Suspended] |
| 164 | ``` |
| 165 | |
| 166 | ### Outputs |
| 167 | |
| 168 | ```yaml |
| 169 | Outputs: |
| 170 | BucketName: |
| 171 | Description: Name of the S3 bucket |
| 172 | Value: !Ref DataBucket |
| 173 | Export: |
| 174 | Name: !Sub '${AWS::StackName}-BucketName' |
| 175 | ``` |
| 176 | |
| 177 | See [references/advanced-configuration.md](references/advanced-configuration.md) for detailed Mappings, Conditions, Parameters, and cross-stack references. |
| 178 | |
| 179 | ## Best Practices |
| 180 | |
| 181 | 1. **Public Access Block**: Always enable for non-stat |