Articles → AWS → Signed Cookies In AWS CloudFront
Signed Cookies In AWS CloudFront
Why Signed Cookies?
Steps
- Create an S3 bucket
- Upload a file on the S3 bucket
- Create a CloudFront distribution
- Create a key pair for the CloudFront
- Download the AWS CloudFront cookie signer from GitHub
- Change the values in cookieSign.js
- Install the required node.js package
- Generate a token
- Access the resource
- Output
Create A S3 Bucket
Click to Enlarge
Click to Enlarge
Upload A File On S3 Bucket
Click to Enlarge
Create A CloudFront Distribution
| Option | Value |
|---|
| Origin domain | [URL of the S3 bucket] |
| Origin Access | Legacy access identities |
| Origin access identity | [origin access identity] |
| Bucket policy | Yes, update the bucket policy |
| Viewer protocol policy | HTTPS only |
| Restrict viewer access | Yes |
| Trusted authorization type | Trusted signer |
| Trusted signers | Self |
Click to Enlarge
Click to Enlarge
Click to Enlarge
Click to Enlarge
Create A Key Pair For The CloudFront
Click to Enlarge
Download AWS CloudFront Cookie Signer From GitHub
Change The Values In Cookiesign.Js
| Variable | Value |
|---|
| keyPairId | Your access key Id that was generated in the previous step. |
| privateKey | Content of the private key value pair file downloaded earlier. The file starts with the word pk. |
| cfUrl | CloudFront URL. |
| expiry | The expiry date of the future. |
Click to Enlarge
Install Required Node.Js Package
npm install aws-sdk
npm install express
npm install body-parser
Generate A Token
http://localhost:3000/getSignedCookie
Click to Enlarge
Access The Resource
Click to Enlarge
Output
Click to Enlarge
| Posted By - | Karan Gupta |
| |
| Posted On - | Monday, March 21, 2022 |