Create First CloudFormation Stack
Prepare for CDK Project
- Delete the Fcj2024CdkStack file that was previously created.

- Remove unnecessary comments from the file.

- Create a new stack by creating a new Java file named EcrStack.java.

Using CDK, you can write code to create stacks and CloudFormation resources. Your code will be compiled into corresponding CloudFormation templates, which can then be deployed by CloudFormation as usual.
- Once the file is created, have the
EcrStack class inherit from the Stack class provided by the AWS CDK library.

- Create the stack constructor.

- Create an AWS resource through a repository:
-
First, declare the productsServiceRepository property with the following line:
private final Repository productsServiceRepository;
-
Then initialize the productsServiceRepository property in the constructor:
this.productsServiceRepository = new Repository(this, "ProductsService", RepositoryProps.builder().build());

- To initialize the Repository, add the following parameters:
- this: Refers to the current class object (usually a CDK stack).
- “ProductsService”: This is the unique name (ID) of the resource being created. This name must be unique within the scope of a CDK stack.
- Then create the ECR Repository properties using RepositoryProps.builder().build():
builder() is a static method of the RepositoryProps class to initialize a Builder object. This is a preparation step to set properties for RepositoryProps.
- The
build() method on the Builder object is called to finalize the construction process and return a complete RepositoryProps object with the set properties.

- Specify the ECR repository name by adding
.repositoryName("productsservice") before build().

- Add .removalPolicy(RemovalPolicy.DESTROY) to ensure that when the CloudFormation stack is deleted, our resources will also be deleted.

- Allow overriding of images in the ECR repository by enabling IMMUTABLE with imageTagMutability(TagMutability.IMMUTABLE).

- Enable automatic image deletion when the ECR is deleted by adding autoDeleteImages(true).

- Create a getter method:
public Repository getProductsServiceRepository() {
return productsServiceRepository;
}
