From 14390941dae3126302910d341d17db65da2f2a2a Mon Sep 17 00:00:00 2001 From: Tolulope Taiwo Sheriff Date: Wed, 12 Feb 2025 18:03:30 +0100 Subject: [PATCH 1/4] S3 Bucket was created, Application was deployed, CloudFront Distribution and Invalidation created and configured by using AWS CDK --- first-cdk-deployment/.gitignore | 10 +++ first-cdk-deployment/README.md | 58 +++++++++++++ first-cdk-deployment/app.py | 31 +++++++ first-cdk-deployment/cdk.json | 85 +++++++++++++++++++ .../first_cdk_deployment/__init__.py | 0 .../first_cdk_deployment_stack.py | 81 ++++++++++++++++++ first-cdk-deployment/requirements-dev.txt | 1 + first-cdk-deployment/requirements.txt | 2 + first-cdk-deployment/source.bat | 13 +++ first-cdk-deployment/tests/__init__.py | 0 first-cdk-deployment/tests/unit/__init__.py | 0 .../unit/test_first_cdk_deployment_stack.py | 15 ++++ 12 files changed, 296 insertions(+) create mode 100644 first-cdk-deployment/.gitignore create mode 100644 first-cdk-deployment/README.md create mode 100644 first-cdk-deployment/app.py create mode 100644 first-cdk-deployment/cdk.json create mode 100644 first-cdk-deployment/first_cdk_deployment/__init__.py create mode 100644 first-cdk-deployment/first_cdk_deployment/first_cdk_deployment_stack.py create mode 100644 first-cdk-deployment/requirements-dev.txt create mode 100644 first-cdk-deployment/requirements.txt create mode 100644 first-cdk-deployment/source.bat create mode 100644 first-cdk-deployment/tests/__init__.py create mode 100644 first-cdk-deployment/tests/unit/__init__.py create mode 100644 first-cdk-deployment/tests/unit/test_first_cdk_deployment_stack.py diff --git a/first-cdk-deployment/.gitignore b/first-cdk-deployment/.gitignore new file mode 100644 index 000000000..37833f8be --- /dev/null +++ b/first-cdk-deployment/.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/first-cdk-deployment/README.md b/first-cdk-deployment/README.md new file mode 100644 index 000000000..c53f0b50c --- /dev/null +++ b/first-cdk-deployment/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: + +``` +$ python3 -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/first-cdk-deployment/app.py b/first-cdk-deployment/app.py new file mode 100644 index 000000000..c3348b587 --- /dev/null +++ b/first-cdk-deployment/app.py @@ -0,0 +1,31 @@ +#!/usr/bin/env python3 +import os + +import aws_cdk as cdk + +from first_cdk_deployment.first_cdk_deployment_stack import FirstCdkDeploymentStack + + +app = cdk.App() +FirstCdkDeploymentStack(app, "FirstCdkDeploymentStack", + # 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() + + +#!/usr/bin/env python3 diff --git a/first-cdk-deployment/cdk.json b/first-cdk-deployment/cdk.json new file mode 100644 index 000000000..8e95d514b --- /dev/null +++ b/first-cdk-deployment/cdk.json @@ -0,0 +1,85 @@ +{ + "app": "python3 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-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, + "@aws-cdk/aws-s3:keepNotificationInImportedBucket": false, + "@aws-cdk/aws-ecs:enableImdsBlockingDeprecatedFeature": false, + "@aws-cdk/aws-ecs:disableEcsImdsBlocking": true, + "@aws-cdk/aws-ecs:reduceEc2FargateCloudWatchPermissions": true, + "@aws-cdk/aws-dynamodb:resourcePolicyPerReplica": true, + "@aws-cdk/aws-ec2:ec2SumTImeoutEnabled": true, + "@aws-cdk/aws-appsync:appSyncGraphQLAPIScopeLambdaPermission": true, + "@aws-cdk/aws-rds:setCorrectValueForDatabaseInstanceReadReplicaInstanceResourceId": true, + "@aws-cdk/core:cfnIncludeRejectComplexResourceUpdateCreatePolicyIntrinsics": true, + "@aws-cdk/aws-lambda-nodejs:sdkV3ExcludeSmithyPackages": true, + "@aws-cdk/aws-stepfunctions-tasks:fixRunEcsTaskPolicy": true, + "@aws-cdk/aws-ec2:bastionHostUseAmazonLinux2023ByDefault": true, + "@aws-cdk/aws-route53-targets:userPoolDomainNameMethodWithoutCustomResource": true, + "@aws-cdk/aws-elasticloadbalancingV2:albDualstackWithoutPublicIpv4SecurityGroupRulesDefault": true, + "@aws-cdk/aws-iam:oidcRejectUnauthorizedConnections": true, + "@aws-cdk/core:enableAdditionalMetadataCollection": true + } +} diff --git a/first-cdk-deployment/first_cdk_deployment/__init__.py b/first-cdk-deployment/first_cdk_deployment/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/first-cdk-deployment/first_cdk_deployment/first_cdk_deployment_stack.py b/first-cdk-deployment/first_cdk_deployment/first_cdk_deployment_stack.py new file mode 100644 index 000000000..7a43f163c --- /dev/null +++ b/first-cdk-deployment/first_cdk_deployment/first_cdk_deployment_stack.py @@ -0,0 +1,81 @@ +# from aws_cdk import ( +# # Duration, +# Stack, +# # aws_sqs as sqs, +# ) +# from constructs import Construct + +# class FirstCdkDeploymentStack(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 + +# # example resource +# # queue = sqs.Queue( +# # self, "FirstCdkDeploymentQueue", +# # visibility_timeout=Duration.seconds(300), +# # ) + + +from aws_cdk import ( + Stack, + aws_s3 as s3, + aws_cloudfront as cloudfront, + aws_cloudfront_origins as origins, + aws_s3_deployment as s3deploy, + CfnOutput, + RemovalPolicy +) +from constructs import Construct +import os + +class FirstCdkDeploymentStack(Stack): + def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None: + super().__init__(scope, construct_id, **kwargs) + + # Create an S3 bucket to store the website files + website_bucket = s3.Bucket( + self, + "ShopReactBucket", + block_public_access=s3.BlockPublicAccess.BLOCK_ALL, + removal_policy=RemovalPolicy.DESTROY, + auto_delete_objects=True + ) + + # Create CloudFront distribution + distribution = cloudfront.Distribution( + self, + "ShopReactDistribution", + default_behavior=cloudfront.BehaviorOptions( + origin=origins.S3Origin(website_bucket), + viewer_protocol_policy=cloudfront.ViewerProtocolPolicy.REDIRECT_TO_HTTPS, + cache_policy=cloudfront.CachePolicy.CACHING_OPTIMIZED, + ), + default_root_object="index.html", + error_responses=[ + cloudfront.ErrorResponse( + http_status=404, + response_http_status=200, + response_page_path="/index.html" + ) + ] + ) + + # Deploy site contents to S3 + s3deploy.BucketDeployment( + self, + "DeployShopReact", + sources=[s3deploy.Source.asset(os.path.join(os.path.dirname(__file__), "..", "..", "dist"))], + destination_bucket=website_bucket, + distribution=distribution, + distribution_paths=["/*"] + ) + + # Output the CloudFront URL + CfnOutput( + self, + "DistributionDomainName", + value=distribution.distribution_domain_name + ) diff --git a/first-cdk-deployment/requirements-dev.txt b/first-cdk-deployment/requirements-dev.txt new file mode 100644 index 000000000..927094516 --- /dev/null +++ b/first-cdk-deployment/requirements-dev.txt @@ -0,0 +1 @@ +pytest==6.2.5 diff --git a/first-cdk-deployment/requirements.txt b/first-cdk-deployment/requirements.txt new file mode 100644 index 000000000..20ba9d5fd --- /dev/null +++ b/first-cdk-deployment/requirements.txt @@ -0,0 +1,2 @@ +aws-cdk-lib==2.178.1 +constructs>=10.0.0,<11.0.0 diff --git a/first-cdk-deployment/source.bat b/first-cdk-deployment/source.bat new file mode 100644 index 000000000..9e1a83442 --- /dev/null +++ b/first-cdk-deployment/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/first-cdk-deployment/tests/__init__.py b/first-cdk-deployment/tests/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/first-cdk-deployment/tests/unit/__init__.py b/first-cdk-deployment/tests/unit/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/first-cdk-deployment/tests/unit/test_first_cdk_deployment_stack.py b/first-cdk-deployment/tests/unit/test_first_cdk_deployment_stack.py new file mode 100644 index 000000000..156c276f9 --- /dev/null +++ b/first-cdk-deployment/tests/unit/test_first_cdk_deployment_stack.py @@ -0,0 +1,15 @@ +import aws_cdk as core +import aws_cdk.assertions as assertions + +from first_cdk_deployment.first_cdk_deployment_stack import FirstCdkDeploymentStack + +# example tests. To run these tests, uncomment this file along with the example +# resource in first_cdk_deployment/first_cdk_deployment_stack.py +def test_sqs_queue_created(): + app = core.App() + stack = FirstCdkDeploymentStack(app, "first-cdk-deployment") + template = assertions.Template.from_stack(stack) + +# template.has_resource_properties("AWS::SQS::Queue", { +# "VisibilityTimeout": 300 +# }) From 83ab24f5e422f48cadf0eba56329716846ccb468 Mon Sep 17 00:00:00 2001 From: Tolulope Taiwo Sheriff Date: Wed, 12 Feb 2025 18:27:41 +0100 Subject: [PATCH 2/4] modified README.md and added changed to MainLayout --- .gitignore | 13 +++++++++++++ README.md | 4 ++++ src/components/MainLayout/MainLayout.tsx | 2 +- 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 0eac30726..4fc63745e 100644 --- a/.gitignore +++ b/.gitignore @@ -24,3 +24,16 @@ dist-ssr *.njsproj *.sln *.sw? + +*.swp +package-lock.json +__pycache__ +.pytest_cache +.venv +*.egg-info +.env + +# CDK asset staging directory +.cdk.staging +cdk.out + diff --git a/README.md b/README.md index cf86ff472..d28b791ee 100644 --- a/README.md +++ b/README.md @@ -36,3 +36,7 @@ Runs tests in console, in browser or with coverage. ### `lint`, `prettier` Runs linting and formatting for all files in `src` folder. + + +### CDK link +[d3q9g2lmtri9eu.cloudfront.net](https://d3q9g2lmtri9eu.cloudfront.net/) diff --git a/src/components/MainLayout/MainLayout.tsx b/src/components/MainLayout/MainLayout.tsx index ec679471c..05e750695 100755 --- a/src/components/MainLayout/MainLayout.tsx +++ b/src/components/MainLayout/MainLayout.tsx @@ -37,7 +37,7 @@ const MainLayout: React.FC<{ children: React.ReactNode }> = ({ children }) => { color="textSecondary" component="p" > - Thank you for your purchase! + Thank you for your purchase! We Appreciate it From 7eb1af6aec71adf48a9c42cc68accdbc0899b670 Mon Sep 17 00:00:00 2001 From: Tolu Taiwo <54937174+Lunarr0@users.noreply.github.com> Date: Thu, 13 Feb 2025 09:30:16 +0100 Subject: [PATCH 3/4] Update README.md --- README.md | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index d28b791ee..0c3a4a54e 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,12 @@ # React-shop-cloudfront + +### CDK link +[d3q9g2lmtri9eu.cloudfront.net](https://d3q9g2lmtri9eu.cloudfront.net/) + +Screenshot 2025-02-13 at 09 26 36 + + This is frontend starter project for nodejs-aws mentoring program. It uses the following technologies: - [Vite](https://vitejs.dev/) as a project bundler @@ -38,5 +45,3 @@ Runs tests in console, in browser or with coverage. Runs linting and formatting for all files in `src` folder. -### CDK link -[d3q9g2lmtri9eu.cloudfront.net](https://d3q9g2lmtri9eu.cloudfront.net/) From fbe081dda85c0e18a2ae83c7f4c4d486e8636433 Mon Sep 17 00:00:00 2001 From: Tolu Taiwo <54937174+Lunarr0@users.noreply.github.com> Date: Thu, 13 Feb 2025 09:31:14 +0100 Subject: [PATCH 4/4] Update README.md --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 0c3a4a54e..7415e5e6f 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,8 @@ ### CDK link [d3q9g2lmtri9eu.cloudfront.net](https://d3q9g2lmtri9eu.cloudfront.net/) -Screenshot 2025-02-13 at 09 26 36 + +Screenshot 2025-02-13 at 09 26 58 This is frontend starter project for nodejs-aws mentoring program. It uses the following technologies: