Create dynamic CI pipelines for Java and Python projects automatically - AWS Prescriptive Guidance
Services or capabilities described in AWS documentation might vary by Region. To see the differences applicable to the AWS European Sovereign Cloud Region, see the AWS European Sovereign Cloud User Guide.

Create dynamic CI pipelines for Java and Python projects automatically

Aromal Raj Jayarajan, Vijesh Vijayakumaran Nair, MAHESH RAGHUNANDANAN, and Amarnath Reddy, Amazon Web Services

Summary

This pattern shows how to create dynamic continuous integration (CI) pipelines for Java and Python projects automatically by using AWS developer tools.

As technology stacks diversify and development activities increase, it can become difficult to create and maintain CI pipelines that are consistent across an organization. By automating the process in AWS Step Functions, you can help keep your CI pipelines consistent across projects.

To automate the creation of dynamic CI pipelines, this pattern uses the following variable inputs:

  • Programming language (Java or Python only)

  • Pipeline name

  • Required pipeline stages

Note

Step Functions orchestrates pipeline creation by using multiple AWS services. For more information about the AWS services used in this solution, see the Tools section of this pattern.

Prerequisites and limitations

Prerequisites

Limitations

  • This pattern supports Java and Python projects only. The generated pipelines build and test the sample projects with Java 17 (Amazon Corretto) and Python 3.12.

  • The IAM roles provisioned in this pattern are designed to follow the principle of least privilege for the CI pipelines that the solution creates. Update the roles’ permissions based on the specific resources that your CI pipeline needs to create.

  • Deploy one copy of the solution in each AWS account and Region. The IAM roles and policies that the solution creates have fixed names, and the state machine’s permissions are limited to those names.

  • The generated pipelines run only the CI stages that you select. They don’t deploy anything.

Architecture

Target technology stack

  • AWS CloudFormation

  • AWS CodeBuild

  • AWS CodeCommit

  • AWS CodePipeline

  • AWS IAM

  • AWS Key Management Service (AWS KMS)

  • Amazon Simple Storage Service (Amazon S3)

  • AWS Systems Manager

  • AWS Step Functions

  • AWS Lambda

  • Amazon DynamoDB

  • Amazon CloudWatch Logs

Target architecture

The following diagram shows an example workflow for creating dynamic CI pipelines for Java and Python projects automatically by using AWS developer tools.

Workflow to create dynamic CI pipelines for Java and Python projects automatically using AWS tools.

The diagram shows the following workflow:

  1. An AWS user provides the input parameters for CI pipeline creation in JSON format. This input starts a Step Functions workflow (state machine) that creates a CI pipeline by using AWS developer tools.

  2. A Lambda function validates the input, reads a folder named input-references from an Amazon S3 bucket, and generates a buildspec.yml file that defines the CI pipeline stages. The function stores the buildspec.yml file and a zip file in the generated-artifacts folder of the same Amazon S3 bucket. The zip file contains the sample source code and the buildspec.yml file.

  3. Step Functions checks the CI pipeline creation workflow’s dependencies for any changes, and updates the dependencies stack as needed.

  4. Step Functions creates the CI pipeline resources in a CloudFormation stack, including a CodeCommit repository, CodeBuild project, and a CodePipeline pipeline.

  5. The CloudFormation stack creates a CodeCommit repository from the zip file, which contains the sample source code for the selected technology stack (Java or Python) and the buildspec.yml file.

  6. An Amazon DynamoDB table, encrypted with an AWS KMS customer managed key, stores CI pipeline runtime details and the status of each request. If an input isn’t valid or a stack fails, the workflow records the failure in the table and stops.

Automation and scale

  • This pattern supports a single development environment only. To use it across multiple development environments, you must change the configuration.

  • To add support for more than one CloudFormation stack, you can create additional CloudFormation templates. For more information, see Getting started with AWS CloudFormation in the CloudFormation documentation.

Tools

Tools

  • AWS Step Functions is a serverless orchestration service that helps you combine AWS Lambda functions and other AWS services to build business-critical applications.

  • AWS Lambda is a compute service that helps you run code without needing to provision or manage servers. It runs your code only when needed and scales automatically, so you pay only for the compute time that you use.

  • AWS CodeBuild is a fully managed build service that helps you compile source code, run unit tests, and produce artifacts that are ready to deploy.

  • AWS CodeCommit is a version control service that helps you privately store and manage Git repositories, without needing to manage your own source control system.

  • AWS CodePipeline helps you quickly model and configure the different stages of a software release and automate the steps required to release software changes continuously.

  • AWS Identity and Access Management (IAM) helps you securely manage access to your AWS resources by controlling who is authenticated and authorized to use them.

  • AWS Key Management Service (AWS KMS) helps you create and control cryptographic keys to help protect your data.

  • Amazon Simple Storage Service (Amazon S3) is a cloud-based object storage service that helps you store, protect, and retrieve any amount of data.

  • AWS CloudFormation helps you set up AWS resources, provision them quickly and consistently, and manage them throughout their lifecycle across AWS accounts and Regions.

  • Amazon DynamoDB is a fully managed NoSQL database service that provides fast, predictable, and scalable performance.

  • AWS Systems Manager Parameter Store provides secure, hierarchical storage for configuration data management and secrets management.

Code

The code for this pattern is available in the GitHub automated-ci-pipeline-creation repository. The repository contains the AWS CloudFormation templates required to create the target architecture outlined in this pattern.

Amazon CloudWatch Logs helps you centralize the logs from all your systems, applications, and AWS services so that you can monitor them and archive them securely.

Best practices

  • Don't enter credentials (secrets) such as tokens or passwords directly into AWS CloudFormation templates or Step Functions action configurations. If you do, the information appears in the Step Functions execution history and diagnostic logs. Instead, use AWS Secrets Manager to set up and store secrets. Then, reference the secrets stored in Secrets Manager within the CloudFormation templates and Step Functions action configurations as needed. For more information, see What is AWS Secrets Manager in the Secrets Manager documentation.

  • Configure server-side encryption for CodePipeline artifacts stored in Amazon S3. For more information, see Configure server-side encryption for artifacts stored in Amazon S3 for CodePipeline in the CodePipeline documentation.

  • Apply least-privilege permissions when configuring IAM roles. For more information, see Apply least-privilege permissions in the IAM documentation.

  • Make sure that your Amazon S3 bucket is not publicly accessible. For more information, see Configuring block public access setting for your S3 buckets in the Amazon S3 documentation.

  • Make sure that you activate versioning for your Amazon S3 bucket. For more information, see Using versioning in S3 buckets in the Amazon S3 documentation.

  • Use IAM Access Analyzer when configuring IAM policies. The tool provides actionable recommendations to help you author secure and functional IAM policies. For more information, see Using AWS Identity and Access Management Access Analyzer in the IAM documentation.

  • When possible, define specific access conditions when configuring IAM policies.

  • Activate Amazon CloudWatch logging for monitoring and auditing purposes. For more information, see What is Amazon CloudWatch Logs? in the CloudWatch documentation.

  • Restrict write access to the Amazon S3 bucket that holds the templates and the buildspec fragments. Anyone who can change the fragments changes what the generated pipelines run.

  • Don’t add secrets to the buildspec fragments. Use AWS Secrets Manager instead.

Epics

TaskDescriptionSkills required

Create an Amazon S3 bucket.

Create an Amazon S3 bucket (or use an existing bucket) to store the required AWS CloudFormation templates, source code, and input files for the solution.

Block all public access to the bucket, and turn on default encryption and versioning.

For more information, see Step 1: Create your first S3 bucket in the Amazon S3 documentation.

Note

The Amazon S3 bucket must be in the same AWS Region where you deploy the solution.

AWS DevOps

Clone the GitHub repository.

Clone the GitHub automated-ci-pipeline-creation repository by running the following command in a terminal window:

git clone https://github.com/aws-samples/automated-ci-pipeline-creation.git

For more information, see Cloning a repository in the GitHub documentation.

AWS DevOps

Upload the Solution Templates folder from the cloned GitHub repository to your Amazon S3 bucket.

Copy the contents from the cloned Solution-Templates folder and upload them into the Amazon S3 bucket that you created.

For more information, see Uploading objects in the Amazon S3 documentation.

Note

Make sure that you upload the contents of the Solution-Templates folder only. You can upload the files at the Amazon S3 bucket’s root level only.

AWS DevOps
TaskDescriptionSkills required

Create a CloudFormation stack to deploy the solution by using the template.yml file in the cloned GitHub repository.

  1. Sign in to the AWS Management Console and then open the AWS CloudFormation console.

  2. Choose Create stack. A dropdown list appears.

  3. In the dropdown list, select With new resources (standard). The Create stack page opens.

  4. In the Specify template section, select the checkbox next to Upload a template file.

  5. Select Choose file. Then, navigate to the cloned GitHub repository’s root folder and select the template.yml file. Then, choose Open.

  6. Choose Next. The Specify stack details page opens.

  7. In the Parameters section, specify the following parameters:

    • For S3TemplateBucketName, enter the name of the Amazon S3 bucket that you created earlier, which contains the source code and references for this solution. Make sure that the bucket name parameter is in lowercase.

    • For DynamoDBTable, enter a name for the DynamoDB table that the CloudFormation stack creates.

    • For StateMachineName, enter a name for the Step Functions state machine that the CloudFormation stack creates.

  8. Choose Next. The Configure stack options page opens.

  9. On the Configure stack options page, choose Next. Don’t change any of the default values. The Review page opens.

  10. Review the stack creation settings. Select the checkbox to acknowledge that CloudFormation might create IAM resources, and then choose Create stack (or Submit) to launch your stack.

Note

While CloudFormation creates your stack, the stack appears on the Stacks page with a status of CREATE_IN_PROGRESS. Wait for the stack’s status to change to CREATE_COMPLETE before you complete the remaining steps in this pattern.

AWS administrator, AWS DevOps
TaskDescriptionSkills required

Run the AWS Step Functions state machine that you created.

  1. Sign in to the AWS Management Console and then open the Step Functions console.

  2. Open the state machine that you created.

  3. Choose Start execution. Then, enter your input values for the workflow in JSON format (see the following example inputs).

  4. Choose Start execution.

JSON formatting

{ "details": { "tech_stack": "Name of the Tech Stack (python/java)", "project_name": "Name of the Project that you want to create with", "pre_build": "Choose the step if it required in the buildspec.yml file i.e., yes/no", "build": "Choose the step if it required in the buildspec.yml file i.e., yes/no", "post_build": "Choose the step if it required in the buildspec.yml file i.e., yes/no", "reports": "Choose the step if it required in the buildspec.yml file i.e., yes/no" } }
Note

tech_stack must be java or python. project_name must have 1 to 40 letters, numbers, or hyphens. pre_build, build, post_build, and reports must be yes or no. The reports setting only applies when build is yes. If an input isn’t valid or a stack fails, the execution fails and the status in the DynamoDB table is "Pipeline creation failed".

Java JSON input example

{ "details": { "tech_stack": "java", "project_name": "pipeline-java-pjt", "pre_build": "yes", "build": "yes", "post_build": "yes", "reports": "yes" } }

Python JSON input example

{ "details": { "tech_stack": "python", "project_name": "pipeline-python-pjt", "pre_build": "yes", "build": "yes", "post_build": "yes", "reports": "yes" } }
AWS administrator, AWS DevOps

Confirm that the AWS CodeCommit repository for the CI pipeline was created.

  1. Sign in to the AWS Management Console and then open the CodeCommit console.

  2. On the Repositories page, verify that the name of the CodeCommit repository that you created appears in the list of repositories. The repository name uses the format <project_name>-Repo (for example, pipeline-java-pjt-Repo).

  3. Open the CodeCommit repository and verify that the sample source code and the buildspec.yml file were pushed to the main branch.

AWS DevOps

Check the AWS CodeBuild project resources.

  1. Sign in to the AWS Management Console and then open the CodeBuild console.

  2. On the Build projects page, verify that the name of the CodeBuild project that you created appears in the list of projects. The project name uses the format <project_name>-Build (for example, pipeline-java-pjt-Build).

  3. Select the name of your CodeBuild project to open the project. Then, review and validate the following configurations:

    • Project Configuration

    • Source

    • Environment

    • Buildspec

    • Batch Configuration

    • Artifacts

AWS DevOps

Validate the CodePipeline stages.

  1. Sign in to the AWS Management Console and then open the CodePipeline console.

  2. On the Pipelines page, verify that the name of the pipeline that you created appears in the list of pipelines. The pipeline name uses the format <project_name>-Pipeline (for example, pipeline-java-pjt-Pipeline).

  3. Select the name of your pipeline to open the pipeline. Then, review and validate each stage of the pipeline. The pipeline has two stages: CodeCheckout and Build.

AWS DevOps

Confirm that the CI pipeline ran successfully.

  1. In the CodePipeline console, on the Pipelines page, select the name of your pipeline to view the pipeline’s status.

  2. Verify that each stage of the pipeline has a Succeeded status.

AWS DevOps
TaskDescriptionSkills required

Delete the CI pipeline stack.

  1. Sign in to the AWS Management Console and open the CloudFormation console.

  2. Delete the stack named <project_name>-Stack for each project that you created.

  3. In the CodeBuild console, delete the report groups that CodeBuild created for each project. In the CloudWatch console, delete the log group for each project (/aws/codebuild/<project_name>-Build) and the Lambda log group (/aws/lambda/<stack name>-AWSLambdaFunction-...). Lambda creates the Lambda log group automatically, and the stack doesn’t delete it.

AWS DevOps

Delete the CI pipeline’s dependencies.

  1. Empty the Amazon S3 buckets whose names start with deployment-artifact-bucket and deployment-artifact-bucket-logs. Because versioning is turned on, delete all object versions. For more information, see Emptying a bucket in the Amazon S3 documentation.

  2. Delete the stack named pipeline-creation-dependencies-stack. If the deletion fails because the log bucket received new objects, empty the bucket again and retry the deletion.

AWS DevOps

Delete the solution stack and the template bucket.

  1. Delete the stack that you created from the template.yml file. The AWS KMS key that the stack created goes into a pending deletion period and is deleted automatically.

  2. Delete the Amazon S3 bucket that you created in the Configure the prerequisites section of this pattern. For more information, see Deleting a bucket in the Amazon S3 documentation.

AWS DevOps

Related resources