- Parameters
- InputDeveloperName: selects the developer key used to look up the environment in the mapping.
- InputBucketName: the target bucket name that both the bucket resource and the policy reference.
- Mapping
- DevMap associates developer names with an Env value. In this example, Arno → Production, Alice → Development.
- Condition
- IsProd uses FindInMap to fetch the Env for the selected developer and Equals to check if it is “Production”.
- Resources and Condition behavior
- MyS3Bucket: always created with the provided InputBucketName.
- MyPublicReadPolicy: annotated with Condition: IsProd so CloudFormation only creates this bucket policy when IsProd evaluates to true. If false, the resource is not created at all.
| Resource Type | Purpose | Example / Notes |
|---|---|---|
| Parameter | Choose developer to map to environment | InputDeveloperName (Arno, Alice) |
| Mapping | Map developer → environment | DevMap (Arno: Production) |
| Condition | Check if resolved environment is Production | IsProd |
| AWS::S3::Bucket | Create the bucket | MyS3Bucket |
| AWS::S3::BucketPolicy | Grant public-read when Production | MyPublicReadPolicy (Condition: IsProd) |
- Deploy for Production (Arno):
- Deploy for Development (Alice) — policy will NOT be created:
- The bucket policy in this example grants
s3:GetObjectto Principal"*", making objects publicly readable. Only apply this to buckets you explicitly intend to expose. - Account-level or bucket-level S3 Block Public Access settings can override bucket policies and prevent public access even when a policy allows it. Verify Block Public Access settings if you expect objects to be public.
- Using a resource-level Condition is often safer than creating a resource and attempting to toggle its permissions at runtime.
Using a resource-level Condition means the resource is not created if the condition is false. This is safer than creating the resource and trying to toggle its permissions at runtime.
Be cautious: public-read policies can expose sensitive data. Confirm that the bucket contains only intended public assets and that logging, monitoring, and lifecycle policies are in place.
- AWS CloudFormation Conditions: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/conditions-section-structure.html
- S3 Bucket Policy examples: https://docs.aws.amazon.com/AmazonS3/latest/userguide/example-bucket-policies.html
- Amazon S3 Block Public Access: https://docs.aws.amazon.com/AmazonS3/latest/userguide/block-public-access.html
- You can safely include conditional resources in stacks you frequently deploy to non-production environments: when the condition is false, CloudFormation simply omits the resource.
- Confirm that your mapping keys (e.g., developer names) and parameter values are always kept in sync so the Condition resolves deterministically.