Good work. @naokiur. We look forward to working with you in 2020 and this year as well.
After knowing AWS CDK "How do you create this with an AWS CDK ...?" I often think like this.
This time, I will introduce a simple CI / CD environment. Built with AWS CDK (Java).
Get the source, ZIP, just store in S3, Because I use Github for business, Not CodeCommit I made Github the source.
To build CodePipeline Broadly speaking, I was able to build it by creating the following.
Literally, it is a class that represents CodePipeline.
build ()
this class
By cdk deploy
I was able to build a Code Pipeline on AWS.
final Pipeline saveToS3Pipeline = Pipeline.Builder
.create(this, "saveSourceToS3")
.pipelineName("saveSourceToS3")
.stages(new ArrayList<>(Arrays.asList(source, build, deploy)))
.build();
Not limited to this
To generate AWS resources
hoge.Builder.create (Stack class, id). ~ Omitted ~ .build ()
It seems that it can be built with
I feel that it is easy to understand.
CodePipeline requires at least two Stage
s, so
You must specify a List with at least two elements in stages ()
.
If not specified, an error will occur.
This is the Stage class to be set in CodePipeline. This time (although there is not much content) You have created all three stages: Source, Build, and Deploy.
final StageProps source = StageProps.builder()
.stageName("DownloadSourceFromGithub")
.actions(new ArrayList<>(Arrays.asList(github)))
.build();
final StageProps build = StageProps.builder()
.stageName("BuildSource")
.actions(new ArrayList<>(Arrays.asList(codeBuild)))
.build();
final StageProps deploy = StageProps.builder()
.stageName("SaveSourceToS3")
.actions(new ArrayList<>(Arrays.asList(s3)))
.build();
It's not written as hoge.Builder.create (Stack class, id). ~ Omitted ~ .build ()
.
Stage is not an AWS service, but an element of CodePipeline,
This is because it does not appear as a resource
in the CloudFormation stack.
(* This is an individual opinion)
It seems that Stage can have multiple Actions.
A class of Action to be executed in Stage. Create Actions for each Stage. This time (because there is not much content) One for each stage.
final Action github = GitHubSourceAction.Builder
.create()
.actionName("DownloadFromGithub")
.oauthToken(githubToken)
.branch(branchName)
.repo(repoName)
.owner(ownerName)
.output(sourceArtifact)
.build();
final Action codeBuild = CodeBuildAction.Builder
.create()
.actionName("BuildSource")
.project(codeBuildProject)
.input(sourceArtifact)
.outputs(new ArrayList<>(Arrays.asList(buildArtifact)))
.build();
final Action s3 = S3DeployAction.Builder
.create()
.bucket(deployBucket)
.actionName("DeploySourceToS3")
.input(buildArtifact)
.build();
In the codepipeline.actions
package,
Since there is a class according to the Action to be executed,
Generate the required class.
(Currently, it doesn't seem to be all that CodePipeline can do ...)
Action to use Github as Source. It's easy to understand. Set up repositories and branches.
You can also specify an OAuth Token to connect, Use the SecretValue class.
This class is for getting secret information. Get information from Secret in the System Manager parameter store It seems that you can get information from Secret Manager.
This time, set the Token of Github in Secret Manager in advance, I tried to get it.
final SecretsManagerSecretOptions secretOptions = SecretsManagerSecretOptions.builder()
.jsonField("github-token")
.build();
final SecretValue githubToken = SecretValue.secretsManager(
"naokiur-secret",
secretOptions
);
A class that builds CodeBuild for CodePipeline. This time I created buildspec.yml in the repository.
A class for deploying to S3.
I came out in the Action class Also a CodePipeline, This class is specified for Input / Output of each Action. Now you have the image of handing over.
Create the following two Specified for Input / Output of each Action.
final Artifact sourceArtifact = Artifact.artifact("Source");
final Artifact buildArtifact = Artifact.artifact("Build");
Now you have a complete build! !!
final SecretValue githubToken = SecretValue.secretsManager("naokiur-secret");
cdk synthesize
that can generate a CloudFormation file, the relevant item was as follows.
* SecretToken: "{{resolve:secretsmanager:naokiur-secret:SecretString:::}}"
Recommended Posts