Source stage

The Source stage is the first step of any CI/CD pipeline and it represents your source code. This stage is in charge of triggering the pipeline based on new code changes (i.e. git push or pull requests). In this workshop, we will be using AWS CodeCommit as the source provider, but CodePipeline also supports S3, GitHub and Amazon ECR as source providers.

Create new file sls-app/sls_app/pipeline_stack.py and edit this file with below codes.

from aws_cdk import (
    core,
    aws_codecommit,
    aws_codepipeline,
    aws_codepipeline_actions
)

class PipelineStack(core.Stack):
    def __init__(self, scope: core.Construct, id: str, **kwargs) -> None:
        super().__init__(scope, id, **kwargs)
        
        code_repo = aws_codecommit.Repository.from_repository_name(
            self, 
            "CodeRepo", 
            repository_name="sls-app"
        )
        
        source_output = aws_codepipeline.Artifact()
        
        pipeline = aws_codepipeline.Pipeline(self,
            'Pipeline'
        )
        
        pipeline.add_stage(
            stage_name='Source',
            actions=[
                aws_codepipeline_actions.CodeCommitSourceAction(
                    action_name='Source_CodeCommit',
                    repository=code_repo,
                    output=source_output,
                )
            ]
        )

Notice how we define an object source_output as a pipeline artifact, this is necessary for any files that you want CodePipeline to pass to downstream stages. In this case, we want our source code to be passed to the Build stage.

Don’t do a cdk deploy just yet, because a pipeline needs to have at least 2 stages to be created. Lets continue to the next page to add a Build stage first.