Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial commit of the AWS CDK Python Quickstarter #956

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

### Added

- inf-cdk-python-aws, provide first iteration of the AWS CDK Quickstarter ([#956](https://github.com/opendevstack/ods-quickstarters/pull/956))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just wondering about the naming:
There's also a terraform cdk - how would the name of that be
inf-terraform-cdk-python?

Based on that, this one should be
inf-aws-cdk-python :-D

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually someone else requested to remove "aws" because its evident ... but I am open to add that again :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as mentioned, there's also terraform cdk so it's not evident, imho :-D


### Modified

### Fixed
Expand Down
30 changes: 30 additions & 0 deletions inf-cdk-python-aws/Jenkinsfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
def odsNamespace = ''
def odsGitRef = ''
def odsImageTag = ''
def sharedLibraryRef = ''
def agentImageTag = ''

node {
odsNamespace = env.ODS_NAMESPACE ?: 'ods'
odsGitRef = env.ODS_GIT_REF ?: 'master'
odsImageTag = env.ODS_IMAGE_TAG ?: 'latest'
sharedLibraryRef = env.SHARED_LIBRARY_REF ?: odsImageTag
agentImageTag = env.AGENT_IMAGE_TAG ?: odsImageTag
}

library("ods-jenkins-shared-library@${sharedLibraryRef}")

odsQuickstarterPipeline(
imageStreamTag: "${odsNamespace}/jenkins-agent-terraform2:${agentImageTag}",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this agent picked by design (just asking)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since the agent already contains all required libs and tools - yes.
other ideas like a dedicated agent are welcome too ...

) { context ->

// Create a new AWS CDK project by invoking cdk init.
sh("""
mkdir ${context.componentId}/src && cd ${context.componentId}/src
cdk init app --language python
""")

odsQuickstarterStageCopyFiles(context)

odsQuickstarterStageRenderJenkinsfile(context)
}
150 changes: 150 additions & 0 deletions inf-cdk-python-aws/Jenkinsfile.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/* generated jenkins file used for building and deploying AWS-infrastructure in projects */

@Library('ods-jenkins-shared-library@@shared_library_ref@') _

odsComponentPipeline(
imageStreamTag: '@ods_namespace@/jenkins-agent-terraform-2306:@agent_image_tag@',
branchToEnvironmentMapping: [
'master': 'dev',
'feature/': 'dev'
]
) { context ->

/* Account credentials are provided via OpenShift, see AWS Terraform Quickstarter also */
awsAccountIds = [
'dev': '<your_dev_aws_account_id>',
'test': '<your_test_aws_account_id>',
'prod': '<your_prod_aws_account_id>'
]
awsRegions = [
'dev': 'eu-west-1',
'test': 'eu-west-1',
'prod': 'eu-west-1'
]

withEnv([
"CDK_DEFAULT_REGION=${awsRegions[context.environment]}",
"AWS_DEFAULT_REGION=${awsRegions[context.environment]}",
]){
dir('src/') { // directory where your CDK stack is located
stagePrepareVirtualEnv(context)
stageAwsAccountInformation(context, awsAccountIds[context.environment], awsRegions[context.environment])

stageBootstrap(context, awsAccountIds[context.environment], awsRegions[context.environment])
stageUnitTest(context)
stageDiff(context)
stageDeploy(context)
}
}
}

def stagePrepareVirtualEnv(def context){
stage('Prepare Python Virtual Environment') {
sh "python --version"
sh "python -mvenv .venv"
sh "source .venv/bin/activate && pip install -r requirements.txt -r requirements-dev.txt"
}
}

def stageAwsAccountInformation(context, String awsAccountId, String awsRegion) {
stage('AWS account information') {
echo "awsAccountId: ${awsAccountId}"
echo "awsRegion: ${awsRegion}"
echo "Reading credentialsId: ${context.projectId}-cd-aws-access-key-id-${context.environment}"
echo "Reading credentialsId: ${context.projectId}-cd-aws-secret-access-key-${context.environment}"
withCredentials([
string(
credentialsId: "${context.projectId}-cd-aws-access-key-id-${context.environment}",
variable: 'AWS_ACCESS_KEY_ID'),
string(
credentialsId: "${context.projectId}-cd-aws-secret-access-key-${context.environment}",
variable: 'AWS_SECRET_ACCESS_KEY'),
]) {
sh 'env | sort'
sh "aws sts get-caller-identity"
}
}
}

def stageBootstrap(def context, String awsAccountId, String awsRegion) {
stage('Bootstrap') {
withCredentials([
string(
credentialsId: "${context.projectId}-cd-aws-access-key-id-${context.environment}",
variable: 'AWS_ACCESS_KEY_ID'),
string(
credentialsId: "${context.projectId}-cd-aws-secret-access-key-${context.environment}",
variable: 'AWS_SECRET_ACCESS_KEY'),
]) {
def success = sh(script: """
#!/bin/bash -e
source .venv/bin/activate && \
cdk bootstrap aws://${awsAccountId}/${awsRegion}
""", returnStatus: true)
if (success!=0){
error("CDK bootstrap failed")
}
}
}
}

def stageUnitTest(def context) {
stage('Unit Test') {
withCredentials([
string(
credentialsId: "${context.projectId}-cd-aws-access-key-id-${context.environment}",
variable: 'AWS_ACCESS_KEY_ID'),
string(
credentialsId: "${context.projectId}-cd-aws-secret-access-key-${context.environment}",
variable: 'AWS_SECRET_ACCESS_KEY'),
]) {
sh """
#!/bin/bash -e
source .venv/bin/activate
pytest --verbose --junitxml=../build/test-results/test/junit.xml

"""
}
}
}

def stageDiff(def context) {
stage('Diff') {
withCredentials([
string(
credentialsId: "${context.projectId}-cd-aws-access-key-id-${context.environment}",
variable: 'AWS_ACCESS_KEY_ID'),
string(
credentialsId: "${context.projectId}-cd-aws-secret-access-key-${context.environment}",
variable: 'AWS_SECRET_ACCESS_KEY'),
]) {
sh """
#!/bin/bash -e
source .venv/bin/activate
cdk diff
"""
}
}
}

def stageDeploy(def context) {
stage('Deploy') {
withCredentials([
string(
credentialsId: "${context.projectId}-cd-aws-access-key-id-${context.environment}",
variable: 'AWS_ACCESS_KEY_ID'),
string(
credentialsId: "${context.projectId}-cd-aws-secret-access-key-${context.environment}",
variable: 'AWS_SECRET_ACCESS_KEY'),
]) {
sh """
#!/bin/bash -e
source .venv/bin/activate
cdk deploy --ci --require-approval never \\
--context env=${context.environment} \\
--context project=${context.projectId} \\
--context anotherContextKey=anotherContextValue
"""
}
}
}
5 changes: 5 additions & 0 deletions inf-cdk-python-aws/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# CDK Python Quickstarter (inf-cdk-python-aws)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AWS CDK Python ...


Documentation is located in our [official documentation](https://www.opendevstack.org/ods-documentation/ods-quickstarters/latest/index.html)

Please update documentation in the [antora page directory](https://github.com/opendevstack/ods-quickstarters/tree/master/docs/modules/quickstarters/pages)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"image": "ghcr.io/nichtraunzer/terrarium:latest"
}
19 changes: 19 additions & 0 deletions inf-cdk-python-aws/files/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# EditorConfig is awesome: http://EditorConfig.org

# top-most EditorConfig file
root = true

[*]
charset = utf-8
end_of_line = lf
indent_size = 2
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false ; trimming trailing whitespace may break Markdown

[Makefile]
tab_width = 2
indent_style = tab
5 changes: 5 additions & 0 deletions inf-cdk-python-aws/files/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
reports/install/*
!reports/install/.gitkeep
.devcontainer
.venv
build
62 changes: 62 additions & 0 deletions inf-cdk-python-aws/files/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# ODS AWS CDK Python Quickstarter
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here it is AWS CDK Python ....


This Quickstarter can be used to deploy AWS resources. Its primary usage is to build solutions based on AWS CDK Python.

## What is the AWS CDK?

The AWS Cloud Development Kit (CDK) is an open-source software development framework that allows you to define cloud infrastructure in code. It provides a high-level object-oriented abstraction to define AWS resources imperatively using programming languages like TypeScript, JavaScript, Python, Java, and C#.

## AWS CDK with Python

AWS CDK supports Python as one of the programming languages to define and manage cloud infrastructure. You can use Python to write your infrastructure code, which will be compiled into AWS CloudFormation templates. This allows you to leverage Python's features and libraries to create, modify, and manage your AWS resources.

## How to get started?

The Quickstarter will automatically create a new AWS CDK Python project in your component repository.
In order to develop, the Python package installer, pip, and virtual environment manager, virtualenv, are required. The provided ODS agent comes already installed with these tools.

If you want to develop locally, make sure those tools are available in your environment, too. To run your code locally execute the following commands:

```
$ python -mvenv .venv
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be aligned and use Makefile -> make init?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

interesting topic - originally i proposed that but some colleagues do not like a make target approach ...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be aligned and use Makefile -> make init?

Personally I don't like the Makfile approach. It is hard to have that available on a local machine.

interesting topic - originally i proposed that but some colleagues do not like a make target approach ...

Guilty as charged!

$ cd src
$ source ../.venv/bin/activate && pip install -r requirements.txt -r requirements-dev.txt
```
You may want to to verify you code by running ...
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo:

You may want to verify your code by running:

```
$ pytest
```

Note that your code will interact with *AWS*. It is up to you to provide sufficient configuration to enable these interactions. Here is an example for *AWS* that uses environment variables (via [Configuring the AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html)):

```
$ export AWS_ACCESS_KEY_ID=...
$ export AWS_SECRET_ACCESS_KEY=...
$ export AWS_DEFAULT_REGION=us-east-1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd use eu-west-1 as the defaults above also direct to eu-west-1.

```

## Brief Example

Here's a simple example of using AWS CDK with Python to create an Amazon S3 bucket:
```
from aws_cdk import core
from aws_cdk.aws_s3 import Bucket

class MyS3BucketStack(core.Stack):

def __init__(self, scope: core.Construct, id: str, **kwargs) -> None:
super().__init__(scope, id, **kwargs)

# Create an Amazon S3 bucket
Bucket(self, "MyBucket", versioned=True)

app = core.App()
MyS3BucketStack(app, "MyS3BucketStack")
app.synth()
```
In this example, we import the necessary modules, define a stack class, create an S3 bucket with versioning enabled, and then synthesize the stack to generate the CloudFormation template.

## Problems? Questions? Suggestions?

In case of problems, questions or suggestions, feel free to file an issue with the respective project's repository. Thanks!

7 changes: 7 additions & 0 deletions inf-cdk-python-aws/files/metadata.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
name: AWS CDK (Python)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here it is AWS CDK (Python)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jajajajaja ...

# yamllint disable-line rule:line-length
description: "The AWS CDK lets you build reliable, scalable, cost-effective applications in the cloud with the considerable expressive power of a programming language."
supplier: https://docs.aws.amazon.com/cdk/v2/guide/home.html
version: 4.x
type: ods-infra
2 changes: 2 additions & 0 deletions inf-cdk-python-aws/files/release-manager.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
dependencies: []
1 change: 1 addition & 0 deletions inf-cdk-python-aws/files/reports/install/.gitkeep
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/report.*
1 change: 1 addition & 0 deletions inf-cdk-python-aws/files/src/requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pytest==6.2.5