Skip to content

Commit

Permalink
upgrade to AWS SDK v3
Browse files Browse the repository at this point in the history
  • Loading branch information
pizzacat83 committed Mar 26, 2023
1 parent d098f6e commit 5f84cab
Show file tree
Hide file tree
Showing 11 changed files with 1,405 additions and 27 deletions.
7 changes: 6 additions & 1 deletion aws/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,17 @@
"test:ci": "tsc --noEmit && yarn run lint:ci && jest --verbose --coverage"
},
"dependencies": {
"@aws-sdk/client-dynamodb": "^3.299.0",
"@aws-sdk/client-s3": "^3.299.0",
"@aws-sdk/client-sns": "^3.299.0",
"@aws-sdk/client-ssm": "^3.299.0",
"@aws-sdk/client-sts": "^3.299.0",
"@aws-sdk/lib-dynamodb": "^3.299.0",
"@aws/dynamodb-data-mapper": "^0.7.3",
"@aws/dynamodb-data-mapper-annotations": "^0.7.3",
"@slack/web-api": "6",
"@tsconfig/node18": "^1.0.1",
"@types/common-tags": "^1.8.1",
"aws-sdk": "^2.1301.0",
"axios": "~0.23.0",
"cheerio": "^1.0.0-rc.10",
"common-tags": "^1.8.0",
Expand Down
9 changes: 7 additions & 2 deletions aws/serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ custom:
includeModules:
nodeModulesRelativeDir: '..'
forceExclude:
- aws-sdk
- '@aws-sdk/client-dynamodb'
- '@aws-sdk/client-s3'
- '@aws-sdk/client-sns'
- '@aws-sdk/client-ssm'
- '@aws-sdk/client-sts'
- '@aws-sdk/lib-dynamodb'
excludeFiles: "**/*.test.ts"
dynamodbConfig:
tableName:
Expand Down Expand Up @@ -53,7 +58,7 @@ custom:

provider:
name: aws
runtime: nodejs14.x
runtime: nodejs18.x
stage: ${self:custom.stage}
region: us-east-1
memorySize: 128
Expand Down
2 changes: 1 addition & 1 deletion aws/serverless/lib/sts.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { STS } = require('aws-sdk');
const { STS } = require('@aws-sdk/client-sts');
const sts = new STS();

module.exports.accountId = async () => (await sts.getCallerIdentity().promise()).Account;
6 changes: 4 additions & 2 deletions aws/src/lib/dynamodb.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import AWS from 'aws-sdk';
import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
import { DynamoDBDocumentClient as DocumentClient } from '@aws-sdk/lib-dynamodb';
import { isReal, stage } from './stages';

export const db = new AWS.DynamoDB.DocumentClient(
const client = new DynamoDBClient(
isReal(stage) ?
{} :
{
region: 'localhost',
endpoint: 'http://localhost:8000',
},
);
export const db = DocumentClient.from(client);
6 changes: 3 additions & 3 deletions aws/src/lib/envvar/prod.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import assert from 'assert';
import AWS from 'aws-sdk';
import { SSM } from '@aws-sdk/client-ssm';
import 'source-map-support/register';
import { stage } from '../stages';
import type { EnvVar, EnvVarKey } from './base';

class EnvVarProd implements EnvVar {
ssm = new AWS.SSM();
ssm = new SSM({});
cache = new Map<EnvVarKey, Promise<string>>();

private async fetch(key: EnvVarKey): Promise<string> {
Expand All @@ -14,7 +14,7 @@ class EnvVarProd implements EnvVar {
Name: `/ap2020bot/${stage}/${key}`,
WithDecryption: true,
/* eslint-enable @typescript-eslint/naming-convention */
}).promise();
});
const value = res?.Parameter?.Value;
assert(value !== undefined);
return value;
Expand Down
14 changes: 8 additions & 6 deletions aws/src/lib/s3.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import AWS from 'aws-sdk';
import { S3 } from '@aws-sdk/client-s3';
import { isReal, stage } from './stages';

export const s3 = isReal(stage) ?
new AWS.S3() :
new AWS.S3({
s3ForcePathStyle: true,
accessKeyId: 'S3RVER',
secretAccessKey: 'S3RVER',
new S3({}) :
new S3({
forcePathStyle: true,
credentials: {
accessKeyId: 'S3RVER',
secretAccessKey: 'S3RVER',
},
endpoint: 'http://localhost:3003',
});

Expand Down
2 changes: 1 addition & 1 deletion aws/src/lib/sns.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SNS } from 'aws-sdk';
import { SNS } from '@aws-sdk/client-sns';
import { isReal, stage } from './stages';

export const sns = new SNS(
Expand Down
4 changes: 3 additions & 1 deletion aws/src/slack/events/attribute.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { EventPayload } from '@/lib/slack/events/types';
import type { MessageAttributeMap, MessageAttributeValue } from 'aws-sdk/clients/sns';
import type { MessageAttributeValue } from '@aws-sdk/client-sns';

type MessageAttributeMap = Record<string, MessageAttributeValue>;

const toAttr = (str: string): MessageAttributeValue => ({
/* eslint-disable @typescript-eslint/naming-convention */
Expand Down
14 changes: 8 additions & 6 deletions aws/src/watch-inshi/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type { MessageAttachment } from '@slack/web-api';
import { source } from 'common-tags';
import type { Option } from 'ts-results';
import { None, Some } from 'ts-results';
import type { AWSError } from 'aws-sdk';
import { S3ServiceException, NoSuchKey } from '@aws-sdk/client-s3';
import { Size, validateSize } from '@/lib/validate';
import assert from 'assert';
import { getColor } from '@/lib/color';
Expand Down Expand Up @@ -67,12 +67,14 @@ const loadOldText = async (): Promise<Option<string>> => {
Bucket: getBucketName('default'),
Key: 'watch-inshi/ist/index.txt',
/* eslint-enable @typescript-eslint/naming-convention */
}).promise();
});
assert(res.Body !== undefined);
return Some(res.Body.toString());
} catch (error_) {
const error = error_ as AWSError;
if (['AccessDenied', 'NoSuchKey'].includes(error.code)) {
} catch (error) {
if (error instanceof NoSuchKey) {
return None;
}
if (error instanceof S3ServiceException && error.name === 'AccessDenied') {
return None;
}
throw error;
Expand All @@ -95,7 +97,7 @@ const saveNewText = async (text: string): Promise<void> => {
Key: 'watch-inshi/ist/index.txt',
Body: buf,
/* eslint-enable @typescript-eslint/naming-convention */
}).promise();
});
};

/**
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
{
"name": "@ap2020/bot",
"version": "1.0.0",
"workspaces": ["aws"],
"workspaces": [
"aws"
],
"main": "index.js",
"repository": "https://github.com/ap2020/ap2020bot.git",
"author": "ap2020",
Expand Down
Loading

0 comments on commit 5f84cab

Please sign in to comment.