Last active
August 25, 2021 13:47
-
-
Save jreijn/390f0d784a4b97afd0feacf2be19a9a9 to your computer and use it in GitHub Desktop.
App Runner CDK
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public ECRBasedAppRunnerService(Construct scope, String id, Role role) { | |
super(scope, id); | |
CfnService.HealthCheckConfigurationProperty healthCheckConfigurationProperty = | |
new CfnService.HealthCheckConfigurationProperty.Builder() | |
.path("/actuator/health") | |
.protocol("HTTP") | |
.interval(10) | |
.timeout(5) | |
.healthyThreshold(5) | |
.unhealthyThreshold(1) | |
.build(); | |
CfnService.AuthenticationConfigurationProperty authenticationConfigurationProperty = | |
CfnService.AuthenticationConfigurationProperty.builder().accessRoleArn(role.getRoleArn()).build(); | |
CfnService.ImageRepositoryProperty imageRepositoryProperty = CfnService.ImageRepositoryProperty.builder() | |
.imageIdentifier("1234567890.dkr.ecr.eu-west-1.amazonaws.com/demo-quotes-service:latest") | |
.imageConfiguration(CfnService.ImageConfigurationProperty.builder() | |
.port("8080") | |
.build()) | |
.imageRepositoryType("ECR") | |
.build(); | |
CfnService.SourceConfigurationProperty sourceConfigurationProperty = CfnService.SourceConfigurationProperty.builder() | |
.imageRepository(imageRepositoryProperty) | |
.authenticationConfiguration(authenticationConfigurationProperty) | |
.autoDeploymentsEnabled(true) | |
.build(); | |
CfnService cfnService = CfnService.Builder.create(this, "AppRunnerService") | |
.serviceName("demo-quotes-service") | |
.instanceConfiguration(CfnService.InstanceConfigurationProperty.builder() | |
.cpu("1 vCPU") | |
.memory("2 GB") | |
.build()) | |
.sourceConfiguration(sourceConfigurationProperty) | |
.healthCheckConfiguration(healthCheckConfigurationProperty) | |
.build(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Role.Builder.create(this, "AppRunnerECRRepositoryRole") | |
.assumedBy(new ServicePrincipal("build.apprunner.amazonaws.com", ServicePrincipalOpts.builder().build())) | |
.managedPolicies( | |
List.of( | |
ManagedPolicy.fromManagedPolicyArn( | |
this, | |
"AWSAppRunnerServicePolicyForECRAccessPolicy", | |
"arn:aws:iam::aws:policy/service-role/AWSAppRunnerServicePolicyForECRAccess" | |
) | |
) | |
) | |
.build(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public ProjectBuildPipeline(Construct scope, String id, ProjectBuildPipelineProps props) { | |
super(scope, id); | |
PipelineProject springBootNativePipelineProject = PipelineProject.Builder.create(this, "SpringBootNativeProject") | |
.environment(BuildEnvironment.builder() | |
// Use standard 4 because otherwise you seem to get a docker error | |
.buildImage(LinuxBuildImage.STANDARD_4_0) | |
// Spring Native is quite a heavy user for memory and CPU, so pick a large instance | |
.computeType(ComputeType.LARGE) | |
.privileged(true) | |
.build()) | |
// Build from the buildspec within the project | |
.buildSpec(BuildSpec.fromSourceFilename("buildspec.yml")) | |
.environmentVariables( | |
Map.of("REPOSITORY_URI", | |
BuildEnvironmentVariable.builder() | |
.type(BuildEnvironmentVariableType.PLAINTEXT) | |
.value(props.getRepository().getRepositoryUri()) | |
.build())) | |
.cache(Cache.local(LocalCacheMode.DOCKER_LAYER)) | |
.build(); | |
Pipeline buildPipeline = Pipeline.Builder.create(this, "apprunner-pipeline-project").build(); | |
Artifact appSourceOutput = Artifact.artifact("APP_SOURCE"); | |
buildPipeline | |
.addStage( | |
StageOptions.builder() | |
.stageName("Source") | |
.actions(List.of( | |
CodeStarConnectionsSourceAction.Builder.create() | |
.actionName("GitHubAppSource") | |
.output(appSourceOutput) | |
.owner("jreijn") | |
.repo("sb-native-app-runner-demo") | |
.branch("master") | |
.connectionArn("arn:aws:codestar-connections:eu-west-1:123456789102:connection/e93cc05a-922c-4833-b963-6cd991bd7b12") | |
.build() | |
)) | |
.build() | |
); | |
buildPipeline | |
.addStage(StageOptions.builder() | |
.stageName("Build") | |
.actions(List.of( | |
CodeBuildAction.Builder.create() | |
.actionName("Build") | |
.project(springBootNativePipelineProject) | |
.input(appSourceOutput) | |
.build() | |
)).build()); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
version: 0.2 | |
phases: | |
pre_build: | |
commands: | |
- echo Logging in to Amazon ECR... | |
- aws --version | |
- $(aws ecr get-login --region $AWS_DEFAULT_REGION --no-include-email) | |
- COMMIT_HASH=$(echo $CODEBUILD_RESOLVED_SOURCE_VERSION | cut -c 1-7) | |
- IMAGE_TAG=${COMMIT_HASH:=latest} | |
build: | |
commands: | |
- echo Running maven build | |
- ./mvnw -pl app package | |
- echo Building the Docker image... | |
- ./mvnw -pl app spring-boot:build-image -Dspring-boot.build-image.imageName=$REPOSITORY_URI:latest | |
- docker tag $REPOSITORY_URI:latest $REPOSITORY_URI:$IMAGE_TAG | |
post_build: | |
commands: | |
- echo Build completed on `date` | |
- echo Pushing the Docker images... | |
- docker push $REPOSITORY_URI:latest | |
- docker push $REPOSITORY_URI:$IMAGE_TAG |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//ECR Repository | |
return Repository.Builder.create(this, "AppRunnerRepository") | |
.imageScanOnPush(true) | |
.imageTagMutability(TagMutability.MUTABLE) | |
.removalPolicy(RemovalPolicy.DESTROY) | |
.repositoryName("demo-quotes-service") | |
.build(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CfnOutput.Builder.create(this, "serviceUrl").value("https://" + cfnService.getServiceUrl()).build(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment