diff --git a/cdk/README.md b/cdk/README.md index e015fe1..83c691f 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 0000000..84b90cb --- /dev/null +++ b/cdk/python/README.md @@ -0,0 +1,46 @@ +# 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) + +## Virtualenv setup and requirements + +To manually create a virtualenv on MacOS and Linux: + +```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. + +```bash +$ source .venv/bin/activate +``` + +If you are on Windows platform, you would activate the virtualenv like this: + +```bash +% .venv\Scripts\activate.bat +``` + +Once the virtualenv is activated, you can install the required dependencies. +```bash +$ pip install -r requirements.txt +``` + +## Building, testing, and deploying the app +* `pytest` test this app +* `cdk deploy` deploy this app diff --git a/cdk/python/app.py b/cdk/python/app.py new file mode 100644 index 0000000..ddea29c --- /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 0000000..762a575 --- /dev/null +++ b/cdk/python/app/lambdapowertuner_stack.py @@ -0,0 +1,40 @@ +from aws_cdk import ( + Stack +) +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 0000000..5cdc5f9 --- /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 0000000..af79f86 --- /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 0000000..9e1a834 --- /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 0000000..7a6f23e --- /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" + } +})