Define an Empty Pipeline #
Now we are ready to define the basics of the pipeline.
Return to the file pipeline_stack.py
and edit as follows:
from constructs import Construct
from aws_cdk import (
Stack,
aws_codecommit as codecommit,
pipelines as pipelines,
)
class WorkshopPipelineStack(Stack):
def __init__(self, scope: Construct, id: str, **kwargs) -> None:
super().__init__(scope, id, **kwargs)
# Creates a CodeCommit repository called 'WorkshopRepo'
repo = codecommit.Repository(
self, "WorkshopRepo", repository_name="WorkshopRepo"
)
pipeline = pipelines.CodePipeline(
self,
"Pipeline",
synth=pipelines.ShellStep(
"Synth",
input=pipelines.CodePipelineSource.code_commit(repo, "main"),
commands=[
"npm install -g aws-cdk", # Installs the cdk cli on Codebuild
"pip install -r requirements.txt", # Instructs Codebuild to install required packages
"cdk synth",
]
),
)
Component Breakdown #
The above code does several things:
pipelines.CodePipeline(...)
: This initializes the pipeline with the required values. This will serve as the base component moving forward. Every pipeline requires at bare minimum:pipelines.ShellStep(...)
: Thesynth
of the pipeline describes the commands necessary to install dependencies, build, and synth the CDK application from source. This should always end in a synth command, for NPM-based projects this is alwaysnpx cdk synth
.- The
input
of the synth step specifies the repository where the CDK source code is stored.
- The
Deploy Pipeline and See Result #
All that’s left to get our pipeline up and running is to commit our changes and run one last cdk deploy.
git add .
git commit -m "MESSAGE" && git push
cdk deploy
CdkPipelines auto-update for each commit in a source repo, so this is the last time we will need to execute this command!
Once deployment is finished, you can go to the CodePipeline console and you will see a new pipeline! If you navigate to it, it should look like this: