Skip to content

Commit

Permalink
Add Aws CDK and Update Readme file
Browse files Browse the repository at this point in the history
  • Loading branch information
Tati-Moon committed Jun 9, 2024
1 parent 8def353 commit 8b27907
Show file tree
Hide file tree
Showing 12 changed files with 779 additions and 49 deletions.
131 changes: 119 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,122 @@ Runs linting and formatting for all files in `src` folder.
[Cloud Front](https://d2zyxqnb5qq3f8.cloudfront.net/)

3) AWS CLI commands:
- aws --version
- aws s3 ls
- aws s3api get-bucket-policy --bucket shop-web-app
- aws cloudfront list-distributions
- curl -o NUL -s -w "Total: %{time_total}s\n" https://d2zyxqnb5qq3f8.cloudfront.net
Total: 0.124938s
- curl -o NUL -s -w "Total: %{time_total}s\n" https://shop-web-app.s3.eu-central-1.amazonaws.com/index.html
Total: 0.177813s
- curl -o $null -s -w "Total: %{time_total}s`n" https://d2zyxqnb5qq3f8.cloudfront.net
Total: 0.036126s`n
- curl -o $null -s -w "Total: %{time_total}s`n" https://shop-web-app.s3.eu-central-1.amazonaws.com/index.html
Total: 0.126337s`n
```sh
`aws --version`
`aws s3 ls`
`aws s3api get-bucket-policy --bucket shop-web-app`
`aws cloudfront list-distributions`
```
```sh
curl -o NUL -s -w "Total: %{time_total}s\n" https://d2zyxqnb5qq3f8.cloudfront.net
# Total: 0.124938s

curl -o NUL -s -w "Total: %{time_total}s\n" https://shop-web-app.s3.eu-central-1.amazonaws.com/index.html
# Total: 0.177813s

curl -o $null -s -w "Total: %{time_total}s\n" https://d2zyxqnb5qq3f8.cloudfront.net
# Total: 0.036126s

curl -o $null -s -w "Total: %{time_total}s\n" https://shop-web-app.s3.eu-central-1.amazonaws.com/index.html
# Total: 0.126337s
```

# Task 2.2

Used [AWS CDK](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-construct-library.html)
1) Created new folder, for example 'cdk'
2) Installed
* `npm install -g aws-cdk`
3) Configured
* `cdk init app --language csharp`
* `cdk bootstrap` or `cdk bootstrap aws://ID/REGION`
(
delete options:
* `aws cloudformation describe-stacks`
* `aws cloudformation delete-stack --stack-name CdkStackLayerStack_NAMES`
* `aws cloudformation delete-stack --stack-name CDKToolkit`
* `npm uninstall -g aws-cdk`
)
4) Added new options to CdkStack.cs and Program.cs
# core for AWS CDK (Program.cs)
```csharp
public static void Main(string[] args)
{
var app = new App();
new CdkStack(app, "CdkStack", new StackProps
{
Env = new Environment
{
Account = "ACCOUNT_ID",
Region = "eu-central-1",
}
});
app.Synth();
}
```
# CdkStack.cs
```csharp
internal CdkStack(Construct scope, string id, IStackProps props = null) : base(scope, id, props)
{
// Create the S3 bucket
var bucket = new Bucket(this, $"{BucketId}-bucket", new BucketProps
{
...
});

var oai = new OriginAccessIdentity(this, "OAI");

// Create an IAM policy statement to allow GetObject action on the S3 bucket
var bucketPolicyStatement = new PolicyStatement(new PolicyStatementProps
{
...
});

bucket.AddToResourcePolicy(bucketPolicyStatement);

// Create CloudFront distribution
var distribution = new CloudFrontWebDistribution(this, $"{BucketId}-distribution", new CloudFrontWebDistributionProps
{
OriginConfigs = new[]
{
...
}
});

// Deploy files to the S3 bucket
new BucketDeployment(this, $"{BucketId}-deployment", new BucketDeploymentProps
{
...
});

// Export CloudFront distribution ID
new CfnOutput(this, "DistributionURL", new CfnOutputProps
{
Value = distribution.DistributionDomainName,
Description = "CloudFront distribution"
});

// Output S3 bucket URL
new CfnOutput(this, "BucketURL", new CfnOutputProps
{
Value = bucket.BucketWebsiteUrl,
Description = "The URL of the S3 bucket website endpoint"
});
}
```

5) Builded C# project
* `dotnet build src` compile this app
* `cdk deploy` deploy this stack to your default AWS account/region
* `cdk diff` compare deployed stack with current state
* `cdk synth` emits the synthesized CloudFormation template
6) Added options to package.json:
"scripts": {
"cdk-deploy": "cdk deploy --app \"cdk_dest/cdk.out\"",
"deploy": "npm run build && npm run cdk-deploy"
}
7) Result of operation 'cdk deploy --all'
[Settings] (img.png)
8) Checked links:
[S3 bucket link](http://shop-web-app-automated.s3-website.eu-central-1.amazonaws.com/) - the 403 error should be shown
[Cloud Front link](https://dfmqzjmg0ul9o.cloudfront.net/) - should be available.
Loading

0 comments on commit 8b27907

Please sign in to comment.