Skip to content

Commit 2f111d9

Browse files
author
Yuriy Bezsonov
committed
feat(infra): Add IAM role for ECR repository creation template
1 parent 6f17fab commit 2f111d9

3 files changed

Lines changed: 29 additions & 4 deletions

File tree

infra/cdk/src/main/java/sample/com/constructs/EcrRegistry.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
import software.amazon.awscdk.CfnTag;
44
import software.amazon.awscdk.services.ecr.CfnRepositoryCreationTemplate;
5+
import software.amazon.awscdk.services.iam.Role;
6+
import software.amazon.awscdk.services.iam.ServicePrincipal;
7+
import software.amazon.awscdk.services.iam.PolicyStatement;
58
import software.constructs.Construct;
69

710
import java.util.List;
@@ -69,12 +72,28 @@ public EcrRegistry(final Construct scope, final String id, final EcrRegistryProp
6972
}
7073
""";
7174

75+
// Create IAM role for ECR repository creation template
76+
Role ecrTemplateRole = Role.Builder.create(this, "TemplateRole")
77+
.roleName(prefix + "-ecr-template-role")
78+
.assumedBy(new ServicePrincipal("ecr.amazonaws.com"))
79+
.build();
80+
81+
ecrTemplateRole.addToPolicy(PolicyStatement.Builder.create()
82+
.actions(List.of(
83+
"ecr:CreateRepository",
84+
"ecr:TagResource",
85+
"ecr:PutLifecyclePolicy"
86+
))
87+
.resources(List.of("*"))
88+
.build());
89+
7290
// Create Repository Creation Template
7391
this.repositoryCreationTemplate = CfnRepositoryCreationTemplate.Builder.create(this, "Template")
7492
.prefix("ROOT") // Applies to all repositories
7593
.appliedFor(List.of("CREATE_ON_PUSH", "REPLICATION"))
7694
.imageTagMutability("MUTABLE")
7795
.lifecyclePolicy(lifecyclePolicyJson)
96+
.customRoleArn(ecrTemplateRole.getRoleArn())
7897
.resourceTags(List.of(
7998
CfnTag.builder()
8099
.key("Environment")

infra/scripts/ide/bootstrap.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ EOF
144144
source /etc/profile.d/workshop.sh
145145

146146
echo "export ACCOUNT_ID=$(aws sts get-caller-identity --output text --query Account)" | tee -a /etc/profile.d/workshop.sh
147+
echo "export AWS_ACCOUNT_ID=\$ACCOUNT_ID" | tee -a /etc/profile.d/workshop.sh
147148
source /etc/profile.d/workshop.sh
148149

149150
echo "Setting PS1..."

infra/scripts/setup/unicorn-store-spring.sh

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ source "$SCRIPT_DIR/../lib/common.sh"
1010
log_info "Setting up Unicorn Store Spring application..."
1111

1212
# Get AWS account and region
13-
AWS_ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text)
14-
AWS_REGION=$(aws configure get region)
15-
ECR_REGISTRY="${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com"
13+
ACCOUNT_ID=${ACCOUNT_ID:-$(aws sts get-caller-identity --query Account --output text)}
14+
AWS_REGION=${AWS_REGION:-$(aws configure get region)}
15+
ECR_REGISTRY="${ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com"
1616
IMAGE_NAME="unicorn-store-spring"
1717

18-
log_info "AWS Account: $AWS_ACCOUNT_ID"
18+
log_info "AWS Account: $ACCOUNT_ID"
1919
log_info "AWS Region: $AWS_REGION"
2020
log_info "ECR Registry: $ECR_REGISTRY"
2121

@@ -34,6 +34,11 @@ log_success "Copied unicorn-store-spring to ~/environment (tests removed)"
3434
# Change to the app directory
3535
cd ~/environment/unicorn-store-spring
3636

37+
# Build the application with Maven
38+
log_info "Building application with Maven..."
39+
mvn clean package -DskipTests -ntp
40+
log_success "Maven build completed"
41+
3742
# Login to ECR
3843
log_info "Logging in to ECR..."
3944
aws ecr get-login-password --region "$AWS_REGION" | docker login --username AWS --password-stdin "$ECR_REGISTRY"

0 commit comments

Comments
 (0)