Skip to content

Commit 26965b0

Browse files
committed
Revert changes to custom resources
1 parent 13ebae1 commit 26965b0

File tree

7 files changed

+113
-98
lines changed

7 files changed

+113
-98
lines changed

lib/plugins/aws/custom-resources/resources/api-gateway-cloud-watch-role/handler.js

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,20 @@
22

33
const { wait, MAX_AWS_REQUEST_TRY } = require('../utils');
44
const { getEnvironment, handlerWrapper } = require('../utils');
5-
const { GetAccountCommand, UpdateAccountCommand } = require('@aws-sdk/client-api-gateway');
65
const {
6+
APIGatewayClient,
7+
GetAccountCommand,
8+
UpdateAccountCommand,
9+
} = require('@aws-sdk/client-api-gateway');
10+
const {
11+
IAMClient,
712
ListAttachedRolePoliciesCommand,
813
CreateRoleCommand,
914
AttachRolePolicyCommand,
1015
} = require('@aws-sdk/client-iam');
11-
const AWSClientFactory = require('../../../../../aws/client-factory');
1216

13-
const awsFactory = new AWSClientFactory({ maxAttempts: MAX_AWS_REQUEST_TRY });
17+
const apiGateway = new APIGatewayClient({ maxAttempts: MAX_AWS_REQUEST_TRY });
18+
const iam = new IAMClient({ maxAttempts: MAX_AWS_REQUEST_TRY });
1419

1520
async function handler(event, context) {
1621
if (event.RequestType === 'Create') {
@@ -27,9 +32,10 @@ async function create(event, context) {
2732
const { RoleArn } = event.ResourceProperties;
2833
const { Partition: partition, AccountId: accountId, Region: region } = getEnvironment(context);
2934

30-
const assignedRoleArn = (
31-
await awsFactory.send('APIGateway', new GetAccountCommand({}), { region })
32-
).cloudwatchRoleArn;
35+
apiGateway.config.region = () => region;
36+
iam.config.region = () => region;
37+
38+
const assignedRoleArn = (await apiGateway.send(new GetAccountCommand({}))).cloudwatchRoleArn;
3339

3440
let roleArn = `arn:${partition}:iam::${accountId}:role/serverlessApiGatewayCloudWatchRole`;
3541
if (RoleArn) {
@@ -43,18 +49,12 @@ async function create(event, context) {
4349

4450
const attachedPolicies = await (async () => {
4551
try {
46-
return (
47-
await awsFactory.send(
48-
'IAM',
49-
new ListAttachedRolePoliciesCommand({ RoleName: roleName }),
50-
{ region }
51-
)
52-
).AttachedPolicies;
52+
return (await iam.send(new ListAttachedRolePoliciesCommand({ RoleName: roleName })))
53+
.AttachedPolicies;
5354
} catch (error) {
5455
if (error.code === 'NoSuchEntity') {
5556
// Role doesn't exist yet, create;
56-
await awsFactory.send(
57-
'IAM',
57+
await iam.send(
5858
new CreateRoleCommand({
5959
AssumeRolePolicyDocument: JSON.stringify({
6060
Version: '2012-10-17',
@@ -70,8 +70,7 @@ async function create(event, context) {
7070
}),
7171
Path: '/',
7272
RoleName: roleName,
73-
}),
74-
{ region }
73+
})
7574
);
7675
return [];
7776
}
@@ -84,13 +83,11 @@ async function create(event, context) {
8483
(policy) => policy.PolicyArn === apiGatewayPushToCloudWatchLogsPolicyArn
8584
)
8685
) {
87-
await awsFactory.send(
88-
'IAM',
86+
await iam.send(
8987
new AttachRolePolicyCommand({
9088
PolicyArn: apiGatewayPushToCloudWatchLogsPolicyArn,
9189
RoleName: roleName,
92-
}),
93-
{ region }
90+
})
9491
);
9592
}
9693
}
@@ -100,8 +97,7 @@ async function create(event, context) {
10097

10198
const updateAccount = async (counter = 1) => {
10299
try {
103-
await awsFactory.send(
104-
'APIGateway',
100+
await apiGateway.send(
105101
new UpdateAccountCommand({
106102
patchOperations: [
107103
{
@@ -110,8 +106,7 @@ async function create(event, context) {
110106
value: roleArn,
111107
},
112108
],
113-
}),
114-
{ region }
109+
})
115110
);
116111
} catch (error) {
117112
if (counter < 10) {

lib/plugins/aws/custom-resources/resources/cognito-user-pool/lib/permissions.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
'use strict';
22

33
const { MAX_AWS_REQUEST_TRY } = require('../../utils');
4-
const { AddPermissionCommand, RemovePermissionCommand } = require('@aws-sdk/client-lambda');
5-
const AWSClientFactory = require('../../../../../../aws/client-factory');
4+
const {
5+
LambdaClient,
6+
AddPermissionCommand,
7+
RemovePermissionCommand,
8+
} = require('@aws-sdk/client-lambda');
69

7-
const awsFactory = new AWSClientFactory({ maxAttempts: MAX_AWS_REQUEST_TRY });
10+
const lambda = new LambdaClient({ maxAttempts: MAX_AWS_REQUEST_TRY });
811

912
function getStatementId(functionName, userPoolName) {
1013
const normalizedUserPoolName = userPoolName.toLowerCase().replace(/[.:*\s]/g, '');
@@ -17,6 +20,7 @@ function getStatementId(functionName, userPoolName) {
1720

1821
async function addPermission(config) {
1922
const { functionName, userPoolName, partition, region, accountId, userPoolId } = config;
23+
lambda.config.region = () => region;
2024

2125
const payload = {
2226
Action: 'lambda:InvokeFunction',
@@ -25,16 +29,17 @@ async function addPermission(config) {
2529
StatementId: getStatementId(functionName, userPoolName),
2630
SourceArn: `arn:${partition}:cognito-idp:${region}:${accountId}:userpool/${userPoolId}`,
2731
};
28-
return awsFactory.send('Lambda', new AddPermissionCommand(payload), { region });
32+
return lambda.send(new AddPermissionCommand(payload));
2933
}
3034

3135
async function removePermission(config) {
3236
const { functionName, userPoolName, region } = config;
37+
lambda.config.region = () => region;
3338
const payload = {
3439
FunctionName: functionName,
3540
StatementId: getStatementId(functionName, userPoolName),
3641
};
37-
return awsFactory.send('Lambda', new RemovePermissionCommand(payload), { region });
42+
return lambda.send(new RemovePermissionCommand(payload));
3843
}
3944

4045
module.exports = {

lib/plugins/aws/custom-resources/resources/cognito-user-pool/lib/user-pool.js

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
const { MAX_AWS_REQUEST_TRY } = require('../../utils');
44
const {
5+
CognitoIdentityProviderClient,
56
ListUserPoolsCommand,
67
DescribeUserPoolCommand,
78
UpdateUserPoolCommand,
89
} = require('@aws-sdk/client-cognito-identity-provider');
9-
const AWSClientFactory = require('../../../../../../aws/client-factory');
1010

11-
const awsFactory = new AWSClientFactory({ maxAttempts: MAX_AWS_REQUEST_TRY });
11+
const cognito = new CognitoIdentityProviderClient({ maxAttempts: MAX_AWS_REQUEST_TRY });
1212

1313
const customSenderSources = ['CustomSMSSender', 'CustomEmailSender'];
1414

@@ -45,18 +45,18 @@ async function findUserPoolByName(config) {
4545
MaxResults: 60,
4646
};
4747

48+
cognito.config.region = () => region;
49+
4850
async function recursiveFind(nextToken) {
4951
if (nextToken) payload.NextToken = nextToken;
50-
return awsFactory
51-
.send('CognitoIdentityProvider', new ListUserPoolsCommand(payload), { region })
52-
.then((result) => {
53-
const matches = result.UserPools.filter((pool) => pool.Name === userPoolName);
54-
if (matches.length) {
55-
return matches.shift();
56-
}
57-
if (result.NextToken) return recursiveFind(result.NextToken);
58-
return null;
59-
});
52+
return cognito.send(new ListUserPoolsCommand(payload)).then((result) => {
53+
const matches = result.UserPools.filter((pool) => pool.Name === userPoolName);
54+
if (matches.length) {
55+
return matches.shift();
56+
}
57+
if (result.NextToken) return recursiveFind(result.NextToken);
58+
return null;
59+
});
6060
}
6161

6262
return recursiveFind();
@@ -65,18 +65,18 @@ async function findUserPoolByName(config) {
6565
async function getConfiguration(config) {
6666
const { region } = config;
6767

68+
cognito.config.region = () => region;
69+
6870
return findUserPoolByName(config).then((userPool) =>
69-
awsFactory.send(
70-
'CognitoIdentityProvider',
71-
new DescribeUserPoolCommand({ UserPoolId: userPool.Id }),
72-
{ region }
73-
)
71+
cognito.send(new DescribeUserPoolCommand({ UserPoolId: userPool.Id }))
7472
);
7573
}
7674

7775
async function updateConfiguration(config) {
7876
const { lambdaArn, userPoolConfigs, region } = config;
7977

78+
cognito.config.region = () => region;
79+
8080
return getConfiguration(config).then((res) => {
8181
const UserPoolId = res.UserPool.Id;
8282
let { LambdaConfig } = res.UserPool;
@@ -102,15 +102,15 @@ async function updateConfiguration(config) {
102102
LambdaConfig,
103103
});
104104

105-
return awsFactory.send('CognitoIdentityProvider', new UpdateUserPoolCommand(updatedConfig), {
106-
region,
107-
});
105+
return cognito.send(new UpdateUserPoolCommand(updatedConfig));
108106
});
109107
}
110108

111109
async function removeConfiguration(config) {
112110
const { lambdaArn, region } = config;
113111

112+
cognito.config.region = () => region;
113+
114114
return getConfiguration(config).then((res) => {
115115
const UserPoolId = res.UserPool.Id;
116116
let { LambdaConfig } = res.UserPool;
@@ -124,9 +124,7 @@ async function removeConfiguration(config) {
124124
LambdaConfig,
125125
});
126126

127-
return awsFactory.send('CognitoIdentityProvider', new UpdateUserPoolCommand(updatedConfig), {
128-
region,
129-
});
127+
return cognito.send(new UpdateUserPoolCommand(updatedConfig));
130128
});
131129
}
132130

lib/plugins/aws/custom-resources/resources/event-bridge/lib/event-bridge.js

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,30 @@
33
const { MAX_AWS_REQUEST_TRY } = require('../../utils');
44
const { getEventBusName, getEventBusTargetId } = require('./utils');
55
const {
6+
EventBridgeClient,
67
CreateEventBusCommand,
78
DeleteEventBusCommand,
89
PutRuleCommand,
910
DeleteRuleCommand,
1011
PutTargetsCommand,
1112
RemoveTargetsCommand,
1213
} = require('@aws-sdk/client-eventbridge');
13-
const AWSClientFactory = require('../../../../../../aws/client-factory');
1414

15-
const awsFactory = new AWSClientFactory({ maxAttempts: MAX_AWS_REQUEST_TRY });
15+
const eventBridge = new EventBridgeClient({ maxAttempts: MAX_AWS_REQUEST_TRY });
1616

1717
async function createEventBus(config) {
1818
const { eventBus, region } = config;
1919

20+
eventBridge.config.region = () => region;
21+
2022
if (eventBus) {
2123
if (eventBus.startsWith('arn')) {
2224
return Promise.resolve();
2325
}
24-
return awsFactory.send(
25-
'EventBridge',
26+
return eventBridge.send(
2627
new CreateEventBusCommand({
2728
Name: eventBus,
28-
}),
29-
{ region }
29+
})
3030
);
3131
}
3232
return Promise.resolve();
@@ -35,17 +35,17 @@ async function createEventBus(config) {
3535
async function deleteEventBus(config) {
3636
const { eventBus, region } = config;
3737

38+
eventBridge.config.region = () => region;
39+
3840
if (eventBus) {
3941
if (eventBus.startsWith('arn')) {
4042
return Promise.resolve();
4143
}
4244

43-
return awsFactory.send(
44-
'EventBridge',
45+
return eventBridge.send(
4546
new DeleteEventBusCommand({
4647
Name: eventBus,
47-
}),
48-
{ region }
48+
})
4949
);
5050
}
5151
return Promise.resolve();
@@ -54,39 +54,41 @@ async function deleteEventBus(config) {
5454
async function updateRuleConfiguration(config) {
5555
const { ruleName, eventBus, pattern, schedule, region, state } = config;
5656

57+
eventBridge.config.region = () => region;
58+
5759
const EventBusName = getEventBusName(eventBus);
5860

59-
return awsFactory.send(
60-
'EventBridge',
61+
return eventBridge.send(
6162
new PutRuleCommand({
6263
Name: ruleName,
6364
EventBusName,
6465
EventPattern: JSON.stringify(pattern),
6566
ScheduleExpression: schedule,
6667
State: state,
67-
}),
68-
{ region }
68+
})
6969
);
7070
}
7171

7272
async function removeRuleConfiguration(config) {
7373
const { ruleName, eventBus, region } = config;
7474

75+
eventBridge.config.region = () => region;
76+
7577
const EventBusName = getEventBusName(eventBus);
7678

77-
return awsFactory.send(
78-
'EventBridge',
79+
return eventBridge.send(
7980
new DeleteRuleCommand({
8081
Name: ruleName,
8182
EventBusName,
82-
}),
83-
{ region }
83+
})
8484
);
8585
}
8686

8787
async function updateTargetConfiguration(config) {
8888
const { lambdaArn, ruleName, eventBus, input, inputPath, inputTransformer, region } = config;
8989

90+
eventBridge.config.region = () => region;
91+
9092
const EventBusName = getEventBusName(eventBus);
9193

9294
let target = {
@@ -103,30 +105,29 @@ async function updateTargetConfiguration(config) {
103105
}
104106

105107
return removeTargetConfiguration(config).then(() =>
106-
awsFactory.send(
107-
'EventBridge',
108+
eventBridge.send(
108109
new PutTargetsCommand({
109110
Rule: ruleName,
110111
EventBusName,
111112
Targets: [target],
112-
}),
113-
{ region }
113+
})
114114
)
115115
);
116116
}
117117

118118
async function removeTargetConfiguration(config) {
119119
const { ruleName, eventBus, region } = config;
120+
120121
const EventBusName = getEventBusName(eventBus);
121122

122-
return awsFactory.send(
123-
'EventBridge',
123+
eventBridge.config.region = () => region;
124+
125+
return eventBridge.send(
124126
new RemoveTargetsCommand({
125127
Ids: [getEventBusTargetId(ruleName)],
126128
Rule: ruleName,
127129
EventBusName,
128-
}),
129-
{ region }
130+
})
130131
);
131132
}
132133

0 commit comments

Comments
 (0)