diff --git a/README.md b/README.md index cf86ff472..9166b7a8a 100644 --- a/README.md +++ b/README.md @@ -36,3 +36,6 @@ Runs tests in console, in browser or with coverage. ### `lint`, `prettier` Runs linting and formatting for all files in `src` folder. + +task1.1 https://d1bwom5gl0riaw.cloudfront.net +task1.2 https://rs-task-2.s3.eu-north-1.amazonaws.com/index.html \ No newline at end of file diff --git a/hello-cdk/.gitignore b/hello-cdk/.gitignore new file mode 100644 index 000000000..37833f8be --- /dev/null +++ b/hello-cdk/.gitignore @@ -0,0 +1,10 @@ +*.swp +package-lock.json +__pycache__ +.pytest_cache +.venv +*.egg-info + +# CDK asset staging directory +.cdk.staging +cdk.out diff --git a/hello-cdk/README.md b/hello-cdk/README.md new file mode 100644 index 000000000..e3a001c5e --- /dev/null +++ b/hello-cdk/README.md @@ -0,0 +1,58 @@ + +# Welcome to your CDK Python project! + +This is a blank project for CDK development with Python. + +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 rerun the `pip install -r requirements.txt` +command. + +## Useful commands + + * `cdk ls` list all stacks in the app + * `cdk synth` emits the synthesized CloudFormation template + * `cdk deploy` deploy this stack to your default AWS account/region + * `cdk diff` compare deployed stack with current state + * `cdk docs` open CDK documentation + +Enjoy! diff --git a/hello-cdk/app.py b/hello-cdk/app.py new file mode 100644 index 000000000..abe90cca9 --- /dev/null +++ b/hello-cdk/app.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python3 +import os + +import aws_cdk as cdk + +from hello_cdk.hello_cdk_stack import HelloCdkStack + + +app = cdk.App() +HelloCdkStack(app, "HelloCdkStack", + env=cdk.Environment(account='528273993732', region='us-east-1') + # 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/hello-cdk/cdk.json b/hello-cdk/cdk.json new file mode 100644 index 000000000..ad86fb381 --- /dev/null +++ b/hello-cdk/cdk.json @@ -0,0 +1,70 @@ +{ + "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, + "@aws-cdk/aws-codepipeline-actions:useNewDefaultBranchForCodeCommitSource": true, + "@aws-cdk/aws-cloudwatch-actions:changeLambdaPermissionLogicalIdForLambdaAction": true, + "@aws-cdk/aws-codepipeline:crossAccountKeysDefaultValueToFalse": true, + "@aws-cdk/aws-codepipeline:defaultPipelineTypeToV2": true, + "@aws-cdk/aws-kms:reduceCrossAccountRegionPolicyScope": true, + "@aws-cdk/aws-eks:nodegroupNameAttribute": true, + "@aws-cdk/aws-ec2:ebsDefaultGp3Volume": true, + "@aws-cdk/aws-ecs:removeDefaultDeploymentAlarm": true, + "@aws-cdk/custom-resources:logApiResponseDataPropertyTrueDefault": false + } +} diff --git a/hello-cdk/hello_cdk/__init__.py b/hello-cdk/hello_cdk/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/hello-cdk/hello_cdk/hello_cdk_stack.py b/hello-cdk/hello_cdk/hello_cdk_stack.py new file mode 100644 index 000000000..8f448f628 --- /dev/null +++ b/hello-cdk/hello_cdk/hello_cdk_stack.py @@ -0,0 +1,58 @@ +from aws_cdk import ( + RemovalPolicy, + Stack, + aws_s3 as s3, + aws_cloudfront as cloudfront, + aws_cloudfront_origins as origins, + aws_iam as iam, + aws_s3_deployment as s3deploy +) +from constructs import Construct + +class HelloCdkStack(Stack): + + def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None: + super().__init__(scope, construct_id, **kwargs) + + site_bucket = s3.Bucket(self, + "my-bucket-rs", + block_public_access=s3.BlockPublicAccess.BLOCK_ALL, + removal_policy=RemovalPolicy.DESTROY, + auto_delete_objects=True, + ) + + oai = cloudfront.OriginAccessIdentity( + self, + "OAI-rs", + comment="rs-comment" + ) + + distribution = cloudfront.Distribution( + self, + "MyStaticSiteDistribution", + default_behavior=cloudfront.BehaviorOptions( + origin=origins.S3Origin(site_bucket, origin_access_identity=oai), + viewer_protocol_policy=cloudfront.ViewerProtocolPolicy.REDIRECT_TO_HTTPS + ), + default_root_object="index.html" + ) + + site_bucket.add_to_resource_policy(iam.PolicyStatement( + actions=["s3:GetObject"], + resources=[site_bucket.arn_for_objects("*")], + principals=[iam.ServicePrincipal("cloudfront.amazonaws.com")], + conditions={ + "StringEquals": { + "AWS:SourceArn": f"arn:aws:cloudfront::{self.account}:distribution/{distribution.distribution_id}" + } + } + )) + + site_bucket.grant_read(oai) + + s3deploy.BucketDeployment(self, "DeployWithInvalidation", + sources=[s3deploy.Source.asset("../nodejs-aws-shop-react/dist")], + destination_bucket=site_bucket, + distribution=distribution, + distribution_paths=["/*"] + ) \ No newline at end of file diff --git a/hello-cdk/requirements-dev.txt b/hello-cdk/requirements-dev.txt new file mode 100644 index 000000000..927094516 --- /dev/null +++ b/hello-cdk/requirements-dev.txt @@ -0,0 +1 @@ +pytest==6.2.5 diff --git a/hello-cdk/requirements.txt b/hello-cdk/requirements.txt new file mode 100644 index 000000000..c17236e84 --- /dev/null +++ b/hello-cdk/requirements.txt @@ -0,0 +1,2 @@ +aws-cdk-lib==2.145.0 +constructs>=10.0.0,<11.0.0 diff --git a/hello-cdk/source.bat b/hello-cdk/source.bat new file mode 100644 index 000000000..9e1a83442 --- /dev/null +++ b/hello-cdk/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/hello-cdk/tests/__init__.py b/hello-cdk/tests/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/hello-cdk/tests/unit/__init__.py b/hello-cdk/tests/unit/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/hello-cdk/tests/unit/test_hello_cdk_stack.py b/hello-cdk/tests/unit/test_hello_cdk_stack.py new file mode 100644 index 000000000..52dfd9863 --- /dev/null +++ b/hello-cdk/tests/unit/test_hello_cdk_stack.py @@ -0,0 +1,15 @@ +import aws_cdk as core +import aws_cdk.assertions as assertions + +from hello_cdk.hello_cdk_stack import HelloCdkStack + +# example tests. To run these tests, uncomment this file along with the example +# resource in hello_cdk/hello_cdk_stack.py +def test_sqs_queue_created(): + app = core.App() + stack = HelloCdkStack(app, "hello-cdk") + template = assertions.Template.from_stack(stack) + +# template.has_resource_properties("AWS::SQS::Queue", { +# "VisibilityTimeout": 300 +# }) diff --git a/package.json b/package.json index 632fd8370..9894457a6 100755 --- a/package.json +++ b/package.json @@ -5,6 +5,8 @@ "scripts": { "start": "vite", "build": "tsc && vite build", + "deploy":"npm run build && cd hello-cdk && cdk deploy", + "destroy": "cd hello-cdk && destroy -f", "preview": "npm run build && vite preview", "test": "vitest", "test:ui": "vitest --ui",