From bd2d4b3e5a90414bf27eef606adf0d06ffb7ddf7 Mon Sep 17 00:00:00 2001 From: Nicholas Phair Date: Wed, 10 Apr 2024 04:05:08 -0400 Subject: [PATCH] Bump to Node20 (#72) * Bump to Node20 * Upgrade to aws-js-sdk-v3 --------- Co-authored-by: Connor Kirkpatrick --- source/witch/witch.js | 37 +++++++++++++++++----------------- templates/custom-resource.yaml | 4 ++-- templates/main.yaml | 4 ++-- 3 files changed, 22 insertions(+), 23 deletions(-) diff --git a/source/witch/witch.js b/source/witch/witch.js index 2a8bc48..109b589 100644 --- a/source/witch/witch.js +++ b/source/witch/witch.js @@ -1,18 +1,18 @@ -const aws = require('aws-sdk'); -const fs = require('fs'); -const path = require('path'); +const { S3Client, PutObjectCommand } = require('@aws-sdk/client-s3'); +const fs = require('node:fs'); +const path = require('node:path'); const mime = require('mime-types'); -const https = require('https'); -const url = require('url'); +const https = require('node:https'); +const url = require('node:url'); -const s3 = new aws.S3(); +const s3Client = new S3Client(); const SUCCESS = 'SUCCESS'; const FAILED = 'FAILED'; const { BUCKET } = process.env; -exports.staticHandler = function (event, context) { +exports.staticHandler = (event, context) => { if (event.RequestType !== 'Create' && event.RequestType !== 'Update') { return respond(event, context, SUCCESS, {}); } @@ -23,16 +23,16 @@ exports.staticHandler = function (event, context) { console.log(`${file} -> ${fileType}`); - return s3 - .upload({ + return s3Client.send( + new PutObjectCommand({ Body: fs.createReadStream(file), Bucket: BUCKET, ContentType: fileType, Key: file, ACL: 'private', }) - .promise(); - }), + ); + }) ) .then((msg) => { respond(event, context, SUCCESS, {}); @@ -63,12 +63,11 @@ function respond( responseStatus, responseData, physicalResourceId, - noEcho, + noEcho ) { const responseBody = JSON.stringify({ Status: responseStatus, - Reason: - 'See the details in CloudWatch Log Stream: ' + context.logStreamName, + Reason: `See the details in CloudWatch Log Stream: ${context.logStreamName}`, PhysicalResourceId: physicalResourceId || context.logStreamName, StackId: event.StackId, RequestId: event.RequestId, @@ -91,14 +90,14 @@ function respond( }, }; - const request = https.request(options, function (response) { - console.log('Status code: ' + response.statusCode); - console.log('Status message: ' + response.statusMessage); + const request = https.request(options, (response) => { + console.log(`Status code: ${response.statusCode}`); + console.log(`Status message: ${response.statusMessage}`); context.done(); }); - request.on('error', function (error) { - console.log('send(..) failed executing https.request(..): ' + error); + request.on('error', (error) => { + console.log(`send(..) failed executing https.request(..): ${error}`); context.done(); }); diff --git a/templates/custom-resource.yaml b/templates/custom-resource.yaml index 0342dd8..52ec6ab 100644 --- a/templates/custom-resource.yaml +++ b/templates/custom-resource.yaml @@ -43,7 +43,7 @@ Resources: Properties: ContentUri: ../witch.zip CompatibleRuntimes: - - nodejs16.x + - nodejs20.x CopyRole: Type: 'AWS::IAM::Role' @@ -87,7 +87,7 @@ Resources: Layers: - !Ref CopyLayerVersion Role: !GetAtt CopyRole.Arn - Runtime: nodejs16.x + Runtime: nodejs20.x Timeout: 300 Outputs: diff --git a/templates/main.yaml b/templates/main.yaml index 8261af4..dd80458 100644 --- a/templates/main.yaml +++ b/templates/main.yaml @@ -2,7 +2,7 @@ AWSTemplateFormatVersion: 2010-09-09 Description: | ACFS3 - S3 Static site with CF and ACM https://github.com/aws-samples/amazon-cloudfront-secure-static-site - (uksb-1qnk6ni7b) (version:v0.5) + (uksb-1qnk6ni7b) (version:v0.12) Metadata: AWS::CloudFormation::Interface: @@ -16,7 +16,7 @@ Metadata: Mappings: Solution: Constants: - Version: 'v0.11' + Version: 'v0.12' Rules: OnlyUsEast1: