- Map developer names to metadata that includes the environment.
- Accept the bucket name and developer name via template parameters at stack creation time.
- Use a Condition to detect whether the chosen developer is in Production.
- Create the S3 bucket for all environments, and add a public-read BucketPolicy only when the Condition evaluates to true.
Conditions control whether specific resources or outputs are created. If a resource has a Condition and the condition evaluates to false, CloudFormation will not create that resource.
Mapping and parameters
Define a Mappings block to store per-developer metadata (including the Env key) and add Parameters to accept the bucket name and developer name:| Parameter | Type | Purpose |
|---|---|---|
| InputBucketName | String | The S3 bucket name to create |
| InputDeveloperName | String | Choose which developer mapping to use (controls environment) |
Define the Condition
Add a Conditions block that sets IsProd to true only when the mapped Env equals “Production”. This uses !FindInMap to look up the Env value and !Equals to compare it:- !FindInMap [DevMap, !Ref InputDeveloperName, Env] — fetches the Env value for the provided developer (for example, “Production” or “Testing/development”).
- !Equals [value1, value2] — returns true only when both values match exactly. Here, IsProd becomes true only when the mapped Env is “Production”.
Resources
Create an S3 bucket for every deployment, tag it with Developer and Environment, and create a BucketPolicy resource that only gets created whenIsProd evaluates to true.
| Resource logical ID | Type | Created when |
|---|---|---|
| MyS3Bucket | AWS::S3::Bucket | Always (for any selected developer) |
| MyPublicReadPolicy | AWS::S3::BucketPolicy | Only when IsProd evaluates to true (mapped Env == “Production”) |
- The
Condition: IsProdproperty on the BucketPolicy resource ensures CloudFormation creates the policy only for production mappings. - The policy allows public read access (s3:GetObject) to all objects in the bucket. Only enable public read where it matches your security and compliance requirements.
Be careful when making S3 buckets publicly readable. Ensure this matches your security requirements and compliance policies before enabling public access.
Summary
- Use a Mappings block (DevMap) to store developer metadata including Env.
- Collect user input with Parameters for bucket name and developer selection.
- Use a Conditions block to define
IsProdby combining !FindInMap and !Equals to test for “Production”. - Apply the condition to the BucketPolicy resource so the public-read policy is created only when the selected developer maps to Production.
- AWS CloudFormation — Conditions
- AWS CloudFormation — Intrinsic Functions (!FindInMap, !Equals)
- Amazon S3 Bucket Policies