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

Pipelines #17

Merged
merged 18 commits into from
May 6, 2021
Merged
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
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ env
.cdk.staging
cdk.out
.vscode
local_test.py
local_test.py

docker-access.key
52 changes: 52 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,56 @@

# Welcome to your CDK Python project!

# Deploying

- To deploy to the "hacking" stack, which is the stack that the devs should hack on, run `cdk deploy RenderLambdaStack`.
- To deploy to the "Pre-production" stack, merge the feature into the `develop` branch.
- To deploy to production, merge the feature into the `master` branch.

Examples can be found [here](https://github.com/aws-samples/aws-cdk-examples/tree/master/python)

This is a blank project for Python development with CDK.

The `cdk.json` file tells the CDK Toolkit how to execute your app.

This project is set up like a standard Python project. The initialization
process also creates a virtualenv within this project, stored under the `.venv`
directory. To create the virtualenv it assumes that there is a `python3`
(or `python` for Windows) executable in your path with access to the `venv`
package. If for any reason the automatic creation of the virtualenv fails,
you can create the virtualenv manually.

To manually create a virtualenv on MacOS and Linux:

```
$ python -m venv .venv
```

After the init process completes and the virtualenv is created, you can use the following
step to activate your virtualenv.

```
$ source .venv/bin/activate
```

If you are a Windows platform, you would activate the virtualenv like this:

```
% .venv\Scripts\activate.bat
```

Once the virtualenv is activated, you can install the required dependencies.

```
$ pip install -r requirements.txt
```

At this point you can now synthesize the CloudFormation template for this code.

```
$ cdk synth
```

To add additional dependencies, for example other CDK libraries, just add
them to your `setup.py` file and run the `pipenv update -e .`
command.
Expand Down
28 changes: 9 additions & 19 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,21 @@
import os

from aws_cdk import core as cdk
# For consistency with TypeScript code, `cdk` is the preferred import name for
# the CDK's cdk module. The following line also imports it as `cdk` for use
# with examples from the CDK Developer's Guide, which are in the process of
# being updated to use `cdk`. You may delete this import if you don't need it.

from stack.stack import RenderLambdaStack

from stack.pipelines import RenderLambdaPipeline, ProdRenderLambdaPipeline

app = cdk.App()
RenderLambdaStack(app, "RenderLambdaStack",
# If you don't specify 'env', this stack will be environment-agnostic.
# Account/Region-dependent features and context lookups will not work,
# but a single synthesized template can be deployed anywhere.

# Uncomment the next line to specialize this stack for the AWS Account
# and Region that are implied by the current CLI configuration.

#env=cdk.Environment(account=os.getenv('CDK_DEFAULT_ACCOUNT'), region=os.getenv('CDK_DEFAULT_REGION')),

# Uncomment the next line if you know exactly what Account and Region you
# want to deploy the stack to. */
# Testing Stack
RenderLambdaStack(app, "RenderLambdaStack",
env=cdk.Environment(account='576758376358', region='us-east-1')
)

#env=cdk.Environment(account='123456789012', region='us-east-1'),
# staging pipeline
RenderLambdaPipeline(app, 'RenderLambdaPipeline')

# For more information, see https://docs.aws.amazon.com/cdk/latest/guide/environments.html
)
# production pipeline
ProdRenderLambdaPipeline(app, 'ProdRenderLambdaPipeline')

app.synth()
3 changes: 2 additions & 1 deletion cdk.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"@aws-cdk/aws-secretsmanager:parseOwnedSecretName": true,
"@aws-cdk/aws-kms:defaultKeyPolicies": true,
"@aws-cdk/aws-s3:grantWriteWithoutAcl": true,
"@aws-cdk/aws-ecs-patterns:removeDefaultDesiredCount": true
"@aws-cdk/aws-ecs-patterns:removeDefaultDesiredCount": true,
"@aws-cdk/core:newStyleStackSynthesis": true
}
}
6 changes: 3 additions & 3 deletions lambdas/downloader/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM python:3
FROM public.ecr.aws/bitnami/python:3.8-prod

RUN apt-get update && apt-get install --no-install-recommends -y build-essential ffmpeg \
&& apt-get clean && \
Expand All @@ -13,5 +13,5 @@ RUN pip install awslambdaric
RUN apt-get purge build-essential -y && apt-get autoremove -y

WORKDIR /
ENTRYPOINT [ "/usr/local/bin/python", "-m", "awslambdaric" ]
CMD ["main.handler"]
ENTRYPOINT [ "python", "-m", "awslambdaric" ]
CMD ["main.handler"]
2 changes: 1 addition & 1 deletion scripts/synth.bat
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
@echo off
cdk synth --no-staging > template.yaml
cdk synth DevRenderLambda --no-staging > template.yaml
24 changes: 13 additions & 11 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,19 @@
packages=setuptools.find_packages(where="stack"),

install_requires=[
"aws_cdk.aws_lambda_python==1.95.1",
"aws-cdk.core==1.95.1",
"aws-cdk.aws-lambda==1.95.1",
"aws-cdk.aws-s3==1.95.1",
"aws-cdk.aws-s3-notifications==1.95.1",
"aws-cdk.aws-mediaconvert==1.95.1",
"aws-cdk.aws-lambda-event-sources==1.95.1",
"aws-cdk.aws-lambda-python==1.95.1",
"aws-cdk.aws-ecr==1.95.1",
"aws-cdk.aws-stepfunctions==1.95.1",
"aws-cdk.aws-stepfunctions-tasks==1.95.1",
"aws_cdk.aws_lambda_python==1.101.0",
"aws-cdk.core==1.101.0",
"aws-cdk.aws-lambda==1.101.0",
"aws-cdk.aws-s3==1.101.0",
"aws-cdk.aws-s3-notifications==1.101.0",
"aws-cdk.aws-mediaconvert==1.101.0",
"aws-cdk.aws-lambda-event-sources==1.101.0",
"aws-cdk.aws-lambda-python==1.101.0",
"aws-cdk.aws-ecr==1.101.0",
"aws-cdk.aws-stepfunctions==1.101.0",
"aws-cdk.aws-stepfunctions-tasks==1.101.0",
"aws-cdk.pipelines==1.101.0",
"aws-cdk.aws-codepipeline-actions==1.101.0",
"boto3==1.17.39"
],

Expand Down
17 changes: 17 additions & 0 deletions stack/pipeline_stages.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import typing

from aws_cdk.core import CfnOutput, Construct, Stage, Environment
from stack.stack import RenderLambdaStack

class PreprodStage(Stage):
def __init__(self, scope: Construct, id: str) -> None:
super().__init__(scope, id)

service = RenderLambdaStack(self, 'RenderStack')

class ProdStage(Stage):
def __init__(self, scope: Construct, id: str) -> None:
super().__init__(scope, id)

service = RenderLambdaStack(self, 'RenderLambdaStack')

94 changes: 94 additions & 0 deletions stack/pipelines.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
from aws_cdk import core as cdk
from aws_cdk import pipelines
from aws_cdk import aws_codepipeline_actions as codepipeline_actions
from aws_cdk import aws_codepipeline as codepipeline

from stack.pipeline_stages import PreprodStage, ProdStage
# used this as reference
# https://aws.amazon.com/blogs/developer/cdk-pipelines-continuous-delivery-for-aws-cdk-applications/
Copy link
Member

Choose a reason for hiding this comment

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

Nice - here is another source I saw that was good - https://docs.aws.amazon.com/cdk/latest/guide/codepipeline_example.html

class RenderLambdaPipeline(cdk.Stack):

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



source_artifact = codepipeline.Artifact()
cloudAssemblyArtifact = codepipeline.Artifact()

pipeline = pipelines.CdkPipeline(self, 'RenderLambdaPipeline',
# pipeline name and assembly
pipeline_name='RenderLambdaPipeline',
cloud_assembly_artifact=cloudAssemblyArtifact,

# where the code comes from
source_action=codepipeline_actions.GitHubSourceAction(
action_name='GitHubRenderLambda',
output=source_artifact,
oauth_token=cdk.SecretValue.secrets_manager('github-token', json_field='chand1012'),
owner='pillargg',
repo='render-lambda',
branch='develop'
),

# synthesises the code
# if this were a typescript project
# it would also compile
synth_action=pipelines.SimpleSynthAction(
install_commands=[
'npm i -g aws-cdk',
'pip install -r ./requirements.txt'
],
synth_command="cdk synth",
source_artifact=source_artifact,
cloud_assembly_artifact=cloudAssemblyArtifact,
environment={'privileged': True}
# build_commands=['npm run build']
)
)

pipeline.add_application_stage(PreprodStage(self, 'PreProd'))


class ProdRenderLambdaPipeline(cdk.Stack):
def __init__(self, scope: cdk.Construct, construct_id: str, **kwargs) -> None:
super().__init__(scope, construct_id, **kwargs)



source_artifact = codepipeline.Artifact()
cloudAssemblyArtifact = codepipeline.Artifact()

pipeline = pipelines.CdkPipeline(self, 'RenderLambdaPipeline',
# pipeline name and assembly
pipeline_name='RenderLambdaPipeline',
cloud_assembly_artifact=cloudAssemblyArtifact,

# where the code comes from
source_action=codepipeline_actions.GitHubSourceAction(
action_name='GitHubRenderLambda',
output=source_artifact,
oauth_token=cdk.SecretValue.secrets_manager('github-token', json_field='chand1012'),
Copy link
Member

Choose a reason for hiding this comment

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

is this your personal token? You could probably make a company token on GH

Copy link
Member Author

Choose a reason for hiding this comment

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

Sadly we can't make personal access tokens at a company level, so this will have to do for now.

owner='pillargg',
repo='render-lambda',
branch='master'
),

# synthesises the code
# if this were a typescript project
# it would also compile
synth_action=pipelines.SimpleSynthAction(
install_commands=[
'npm i -g aws-cdk',
'pip install -r ./requirements.txt'
],
synth_command="cdk synth",
source_artifact=source_artifact,
cloud_assembly_artifact=cloudAssemblyArtifact,
environment={'privileged': True}
# build_commands=['npm run build']
)
)

pipeline.add_application_stage(ProdStage(self, 'Prod'))

Loading