aws / aws/aws-cdk

(amplify-alpha): (Java CDK generate build phases in order)

Open
#28,256 1 comment 0 reactions 0 assignees View on GitHub
@aws-cdk/aws-amplify bug effort/medium p3
Dominant language
TypeScript
Stars
12.9k
Forks
4.6k
Avg merge
1d 19h
Merged PRs (30d)
74

Description

### Describe the bug

Java CDK will not generate amplify.yml `phases` in order of `preBuild`, `build`, and `postBuild`.

### Expected Behavior

So I hope if it's possible for CDK Java Package to always generate the amplify.yml `phases` in order of `preBuild`, then `build`, then `postBuild`, regardless of the order in which they are defined by java.utils.Map.

### Current Behavior

I'm currently using CDK Java amplify-alpha service to deploy my next.js frontend. I noticed that after `cdk deploy`, the build process error occurred as
```
# Starting phase: build
2023-12-05T09:42:57.747Z [INFO]: # Executing command: npm run build
2023-12-05T09:43:11.049Z [INFO]: > frontend@0.1.0 build
> next build
endeg. 1. 0 but: apm run build
2023-12-05T09:43:11.055Z [WARNING]: sh: line 1: next: command not found
...
```
Because the amplify.yml defined by java.util.Map does not maintain the order of phases as `preBuild` then `build`, it might generate something as:
```
frontend:
phases:
build:
commands:
- npm run build
preBuild:
commands:
- npm ci
...
```
From my personal understanding, the container will try to run build first, and next has not been installed yet by preBuild command, thus caused the error. I always need to manually adjust the `amplify.yml` afterwards.

### Reproduction Steps

```
App amplifyApp = App.Builder.create(this,"id")
.appName("appName")
.sourceCodeProvider(GitHubSourceCodeProvider.Builder.create()
.owner("owner")
.repository("repo")
.oauthToken(SecretValue.secretsManager("token"))
.build()
)
.autoBranchDeletion(true)
.buildSpec(BuildSpec.fromObjectToYaml(Map.of(
"version","1.0",
"frontend",Map.of(
"phases",Map.of(
"preBuild",Map.of(
"commands", List.of("npm ci")
),
"build",Map.of(
"commands",List.of("npm run build")
)
),
"artifacts",Map.of(
"baseDirectory",".next",
"files", List.of("**/*")
),
"cache", Map.of(
"paths",List.of("node_modules/**/*")
)
)
)
))
.build();
```
Then the amplify.yml might be
```
frontend:
build:
commands:
- npm run build
preBuild:
commands:
- npm install
...
```

### Possible Solution

_No response_

### Additional Information/Context

_No response_

### CDK CLI Version

2.111.0 (build 2ccb59e)

### Framework Version

_No response_

### Node.js Version

20.10.0

### OS

Macbook M1

### Language

Java

### Language Version

Java (21)

### Other information

_No response_

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the issue through the Java CDK BuildSpec.fromObjectToYaml entry point using the provided nested Map and inspecting the generated amplify.yml. Check how the frontend phases are serialized when the input map does not preserve order. Done means generated phases consistently appear as preBuild, build, and postBuild, with the existing commands still intact.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, java, next.js
Domain
build-system, cloud
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.