From 15a683c4b67688eea7872fd59f5a99afa9edfba4 Mon Sep 17 00:00:00 2001 From: Lavanya Tangutur Date: Mon, 4 Mar 2024 12:33:39 -0500 Subject: [PATCH 1/5] Added python cdk --- cdk/README.md | 1 + cdk/python/README.md | 52 ++++++++++++++++ cdk/python/app.py | 28 +++++++++ cdk/python/app/lambdapowertuner_stack.py | 42 +++++++++++++ cdk/python/cdk.json | 61 +++++++++++++++++++ cdk/python/requirements.txt | 2 + cdk/python/source.bat | 13 ++++ .../tests/unit/test_lambdapowertuner_stack.py | 17 ++++++ 8 files changed, 216 insertions(+) create mode 100644 cdk/python/README.md create mode 100644 cdk/python/app.py create mode 100644 cdk/python/app/lambdapowertuner_stack.py create mode 100644 cdk/python/cdk.json create mode 100644 cdk/python/requirements.txt create mode 100644 cdk/python/source.bat create mode 100644 cdk/python/tests/unit/test_lambdapowertuner_stack.py diff --git a/cdk/README.md b/cdk/README.md index e015fe12..83c691f0 100644 --- a/cdk/README.md +++ b/cdk/README.md @@ -6,6 +6,7 @@ Currently we support: - [TypeScript](typescript/README.md) - [C#](csharp/README.md) +- [Python](python/README.md) You can use these projects as a standalone or reuse it within your own CDK projects. diff --git a/cdk/python/README.md b/cdk/python/README.md new file mode 100644 index 00000000..30f0e1df --- /dev/null +++ b/cdk/python/README.md @@ -0,0 +1,52 @@ +# How to deploy the AWS Lambda Power Tuneing using the CDK for Python + +This CDK project deploys *AWS Lambda Power Tuning* using Python. + +You can use the project as a standalone or reuse it within your own CDK projects. + + +## CDK Prerequisites + +See [here](../README.md). + + +## Language specific prerequisites + +- [Python 3.6 or later](https://docs.aws.amazon.com/cdk/v2/guide/getting_started.html#getting_started_prerequisites) +- [Requirements for CDK with Python](https://docs.aws.amazon.com/cdk/v2/guide/work-with-cdk-python.html) + +## Building, testing, and deploying the app + +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 +``` + +cdk synth +pytest +cdk deploy + + +``` + + diff --git a/cdk/python/app.py b/cdk/python/app.py new file mode 100644 index 00000000..ddea29cb --- /dev/null +++ b/cdk/python/app.py @@ -0,0 +1,28 @@ +#!/usr/bin/env python3 +import os + +import aws_cdk as cdk + +from app.lambdapowertuner_stack import TheLambdaPowerTunerStack + + +app = cdk.App() +TheLambdaPowerTunerStack(app, "The-Lambda-Power-Tuner-Stack", + # 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. */ + + #env=cdk.Environment(account='123456789012', region='us-east-1'), + + # For more information, see https://docs.aws.amazon.com/cdk/latest/guide/environments.html + ) + +app.synth() diff --git a/cdk/python/app/lambdapowertuner_stack.py b/cdk/python/app/lambdapowertuner_stack.py new file mode 100644 index 00000000..7529822b --- /dev/null +++ b/cdk/python/app/lambdapowertuner_stack.py @@ -0,0 +1,42 @@ +from aws_cdk import ( + # Duration, + Stack, + # aws_sqs as sqs, +) +from constructs import Construct + +from aws_cdk import aws_sam as sam + + + +class TheLambdaPowerTunerStack(Stack): + + def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None: + super().__init__(scope, construct_id, **kwargs) + + # The code that defines your stack goes here + + + stateMachineConfiguration= { + # "lambdaResource": "*", + # "PowerValues": "128,256,512,1024,1536,3008", + # "visualizationURL": "https://lambda-power-tuning.shw/", + # "totalExecutionTimeout": "300", + # "payloadS3Key":"*", + # "logGroupRetentionInDays":"7", + # "stateMachineNamePrefix":"powerTuningStateMachine", + # "permissionsBoundary":"", + # "payloadS3Bucket":"", + # "layerSdkName":"", + # "securityGroupIds":"", + # "subnetIds":"" + } + + cfn_application =sam.CfnApplication( + self, "SAR", + location={ + "applicationId": "arn:aws:serverlessrepo:us-east-1:451282441545:applications/aws-lambda-power-tuning", + "semanticVersion": "4.3.4" + }, + parameters = stateMachineConfiguration + ) diff --git a/cdk/python/cdk.json b/cdk/python/cdk.json new file mode 100644 index 00000000..5cdc5f99 --- /dev/null +++ b/cdk/python/cdk.json @@ -0,0 +1,61 @@ +{ + "app": "python app.py", + "watch": { + "include": [ + "**" + ], + "exclude": [ + "README.md", + "cdk*.json", + "requirements*.txt", + "source.bat", + "**/__init__.py", + "**/__pycache__", + "tests" + ] + }, + "context": { + "@aws-cdk/aws-lambda:recognizeLayerVersion": true, + "@aws-cdk/core:checkSecretUsage": true, + "@aws-cdk/core:target-partitions": [ + "aws", + "aws-cn" + ], + "@aws-cdk-containers/ecs-service-extensions:enableDefaultLogDriver": true, + "@aws-cdk/aws-ec2:uniqueImdsv2TemplateName": true, + "@aws-cdk/aws-ecs:arnFormatIncludesClusterName": true, + "@aws-cdk/aws-iam:minimizePolicies": true, + "@aws-cdk/core:validateSnapshotRemovalPolicy": true, + "@aws-cdk/aws-codepipeline:crossAccountKeyAliasStackSafeResourceName": true, + "@aws-cdk/aws-s3:createDefaultLoggingPolicy": true, + "@aws-cdk/aws-sns-subscriptions:restrictSqsDescryption": true, + "@aws-cdk/aws-apigateway:disableCloudWatchRole": true, + "@aws-cdk/core:enablePartitionLiterals": true, + "@aws-cdk/aws-events:eventsTargetQueueSameAccount": true, + "@aws-cdk/aws-iam:standardizedServicePrincipals": true, + "@aws-cdk/aws-ecs:disableExplicitDeploymentControllerForCircuitBreaker": true, + "@aws-cdk/aws-iam:importedRoleStackSafeDefaultPolicyName": true, + "@aws-cdk/aws-s3:serverAccessLogsUseBucketPolicy": true, + "@aws-cdk/aws-route53-patters:useCertificate": true, + "@aws-cdk/customresources:installLatestAwsSdkDefault": false, + "@aws-cdk/aws-rds:databaseProxyUniqueResourceName": true, + "@aws-cdk/aws-codedeploy:removeAlarmsFromDeploymentGroup": true, + "@aws-cdk/aws-apigateway:authorizerChangeDeploymentLogicalId": true, + "@aws-cdk/aws-ec2:launchTemplateDefaultUserData": true, + "@aws-cdk/aws-secretsmanager:useAttachedSecretResourcePolicyForSecretTargetAttachments": true, + "@aws-cdk/aws-redshift:columnId": true, + "@aws-cdk/aws-stepfunctions-tasks:enableEmrServicePolicyV2": true, + "@aws-cdk/aws-ec2:restrictDefaultSecurityGroup": true, + "@aws-cdk/aws-apigateway:requestValidatorUniqueId": true, + "@aws-cdk/aws-kms:aliasNameRef": true, + "@aws-cdk/aws-autoscaling:generateLaunchTemplateInsteadOfLaunchConfig": true, + "@aws-cdk/core:includePrefixInUniqueNameGeneration": true, + "@aws-cdk/aws-efs:denyAnonymousAccess": true, + "@aws-cdk/aws-opensearchservice:enableOpensearchMultiAzWithStandby": true, + "@aws-cdk/aws-lambda-nodejs:useLatestRuntimeVersion": true, + "@aws-cdk/aws-efs:mountTargetOrderInsensitiveLogicalId": true, + "@aws-cdk/aws-rds:auroraClusterChangeScopeOfInstanceParameterGroupWithEachParameters": true, + "@aws-cdk/aws-appsync:useArnForSourceApiAssociationIdentifier": true, + "@aws-cdk/aws-rds:preventRenderingDeprecatedCredentials": true + } +} diff --git a/cdk/python/requirements.txt b/cdk/python/requirements.txt new file mode 100644 index 00000000..af79f860 --- /dev/null +++ b/cdk/python/requirements.txt @@ -0,0 +1,2 @@ +aws-cdk-lib==2.104.0 +constructs>=10.0.0,<11.0.0 diff --git a/cdk/python/source.bat b/cdk/python/source.bat new file mode 100644 index 00000000..9e1a8344 --- /dev/null +++ b/cdk/python/source.bat @@ -0,0 +1,13 @@ +@echo off + +rem The sole purpose of this script is to make the command +rem +rem source .venv/bin/activate +rem +rem (which activates a Python virtualenv on Linux or Mac OS X) work on Windows. +rem On Windows, this command just runs this batch file (the argument is ignored). +rem +rem Now we don't need to document a Windows command for activating a virtualenv. + +echo Executing .venv\Scripts\activate.bat for you +.venv\Scripts\activate.bat diff --git a/cdk/python/tests/unit/test_lambdapowertuner_stack.py b/cdk/python/tests/unit/test_lambdapowertuner_stack.py new file mode 100644 index 00000000..7a6f23ec --- /dev/null +++ b/cdk/python/tests/unit/test_lambdapowertuner_stack.py @@ -0,0 +1,17 @@ +import aws_cdk as cdk +import aws_cdk.assertions as assertions + +from app.lambdapowertuner_stack import TheLambdaPowerTunerStack + +# example tests. To run these tests, uncomment this file along with the example +# resource in python/python_stack.py +def test_sar_app_created(): + app = cdk.App() + stack = TheLambdaPowerTunerStack(app, "TheLambdaPowerTunerStack") + template = assertions.Template.from_stack(stack) + + template.has_resource_properties("AWS::Serverless::Application", { + "Location":{ + "ApplicationId": "arn:aws:serverlessrepo:us-east-1:451282441545:applications/aws-lambda-power-tuning" + } +}) From 6b2ba714510613ca367dcb616752cf98608b8382 Mon Sep 17 00:00:00 2001 From: Lavanya Tangutur Date: Tue, 5 Mar 2024 09:11:43 -0500 Subject: [PATCH 2/5] removed commented code and standardized spacing --- cdk/python/README.md | 2 +- cdk/python/app/lambdapowertuner_stack.py | 22 ++++++++++------------ 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/cdk/python/README.md b/cdk/python/README.md index 30f0e1df..45d925d6 100644 --- a/cdk/python/README.md +++ b/cdk/python/README.md @@ -30,7 +30,7 @@ step to activate your virtualenv. $ source .venv/bin/activate ``` -If you are a Windows platform, you would activate the virtualenv like this: +If you are on Windows platform, you would activate the virtualenv like this: ``` % .venv\Scripts\activate.bat diff --git a/cdk/python/app/lambdapowertuner_stack.py b/cdk/python/app/lambdapowertuner_stack.py index 7529822b..762a575d 100644 --- a/cdk/python/app/lambdapowertuner_stack.py +++ b/cdk/python/app/lambdapowertuner_stack.py @@ -1,7 +1,5 @@ -from aws_cdk import ( - # Duration, - Stack, - # aws_sqs as sqs, +from aws_cdk import ( + Stack ) from constructs import Construct @@ -22,14 +20,14 @@ def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None: # "PowerValues": "128,256,512,1024,1536,3008", # "visualizationURL": "https://lambda-power-tuning.shw/", # "totalExecutionTimeout": "300", - # "payloadS3Key":"*", - # "logGroupRetentionInDays":"7", - # "stateMachineNamePrefix":"powerTuningStateMachine", - # "permissionsBoundary":"", - # "payloadS3Bucket":"", - # "layerSdkName":"", - # "securityGroupIds":"", - # "subnetIds":"" + # "payloadS3Key": "*", + # "logGroupRetentionInDays": "7", + # "stateMachineNamePrefix": "powerTuningStateMachine", + # "permissionsBoundary": "", + # "payloadS3Bucket": "", + # "layerSdkName": "", + # "securityGroupIds": "", + # "subnetIds": "" } cfn_application =sam.CfnApplication( From a8de87b3e70bba4d0a5cfead00a4fb2ac29c29e0 Mon Sep 17 00:00:00 2001 From: Lavanya Tangutur Date: Tue, 5 Mar 2024 12:15:40 -0500 Subject: [PATCH 3/5] added a new section for setup --- cdk/python/README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/cdk/python/README.md b/cdk/python/README.md index 45d925d6..580cdac7 100644 --- a/cdk/python/README.md +++ b/cdk/python/README.md @@ -15,7 +15,7 @@ See [here](../README.md). - [Python 3.6 or later](https://docs.aws.amazon.com/cdk/v2/guide/getting_started.html#getting_started_prerequisites) - [Requirements for CDK with Python](https://docs.aws.amazon.com/cdk/v2/guide/work-with-cdk-python.html) -## Building, testing, and deploying the app +## Virtualenv setup and requirements To manually create a virtualenv on MacOS and Linux: @@ -38,10 +38,11 @@ If you are on Windows platform, you would activate the virtualenv like this: Once the virtualenv is activated, you can install the required dependencies. + ``` $ pip install -r requirements.txt ``` - +## Building, testing, and deploying the app cdk synth pytest cdk deploy From 75976a612eead2af938b8ae55317a6842bb3dfbc Mon Sep 17 00:00:00 2001 From: Lavanya Tangutur Date: Tue, 5 Mar 2024 12:32:04 -0500 Subject: [PATCH 4/5] readme changes --- cdk/python/README.md | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/cdk/python/README.md b/cdk/python/README.md index 580cdac7..23b98152 100644 --- a/cdk/python/README.md +++ b/cdk/python/README.md @@ -19,28 +19,20 @@ See [here](../README.md). To manually create a virtualenv on MacOS and Linux: -``` -$ python -m venv .venv -``` +- $ 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 -``` +- $ source .venv/bin/activate If you are on Windows platform, you would activate the virtualenv like this: -``` -% .venv\Scripts\activate.bat -``` +- % .venv\Scripts\activate.bat Once the virtualenv is activated, you can install the required dependencies. - -``` -$ pip install -r requirements.txt +- $ pip install -r requirements.txt ``` ## Building, testing, and deploying the app cdk synth @@ -48,6 +40,6 @@ pytest cdk deploy -``` + From 77cb3fdb6805ba213d99e748d100006efc91d758 Mon Sep 17 00:00:00 2001 From: Alex Casalboni Date: Tue, 5 Mar 2024 19:47:13 +0100 Subject: [PATCH 5/5] Update python/README.md --- cdk/python/README.md | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/cdk/python/README.md b/cdk/python/README.md index 23b98152..84b90cba 100644 --- a/cdk/python/README.md +++ b/cdk/python/README.md @@ -19,27 +19,28 @@ See [here](../README.md). To manually create a virtualenv on MacOS and Linux: -- $ python -m venv .venv +```bash +$ 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 +```bash +$ source .venv/bin/activate +``` If you are on Windows platform, you would activate the virtualenv like this: -- % .venv\Scripts\activate.bat +```bash +% .venv\Scripts\activate.bat +``` Once the virtualenv is activated, you can install the required dependencies. - -- $ pip install -r requirements.txt +```bash +$ pip install -r requirements.txt ``` -## Building, testing, and deploying the app -cdk synth -pytest -cdk deploy - - - - +## Building, testing, and deploying the app +* `pytest` test this app +* `cdk deploy` deploy this app