Skip to content

Commit

Permalink
fix: Check for Serverless Dashboard IAM stack prior to creation (#12147)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mmarzex committed Sep 12, 2023
1 parent 45cbd52 commit bf518b9
Showing 1 changed file with 46 additions and 12 deletions.
58 changes: 46 additions & 12 deletions lib/utils/serverless-dashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,23 @@ const DashboardService = (serverless, options) => {
const integrationSetupProgress = progress.get('main');
let context = {};

/**
* Check for IAM Role Stack to exist
* @returns boolean
*/
const checkIfStackExists = async () => {
try {
const stacks = (
await awsRequest(context, cloudFormationServiceConfig, 'describeStacks', {
StackName: iamRoleStackName,
})
).Stacks;
return stacks.find((stack) => stack.StackName === iamRoleStackName) !== undefined;
} catch (err) {
return false;
}
};

/**
* Wait for IAM Role Stack to be created
* @returns
Expand Down Expand Up @@ -322,7 +339,15 @@ const DashboardService = (serverless, options) => {
log.notice.success('Your AWS account is integrated with Serverless Dashboard');
return true;
}
if (!context.integration) {

const stackExists = await checkIfStackExists();

if (stackExists) {
log.notice.success(
'Your AWS account is currently being integrated with Serverless Dashboard'
);
}
if (!context.integration && !stackExists) {
integrationSetupProgress.notice('Creating IAM Role for Serverless Dashboard');
const { cfnTemplateUrl, params } = await apiRequest(
`/api/integrations/aws/initial?orgId=${context.org.orgId}`,
Expand All @@ -333,17 +358,26 @@ const DashboardService = (serverless, options) => {
}
);

await awsRequest(context, cloudFormationServiceConfig, 'createStack', {
Capabilities: ['CAPABILITY_NAMED_IAM'],
StackName: iamRoleStackName,
TemplateURL: cfnTemplateUrl,
Parameters: [
{ ParameterKey: 'AccountId', ParameterValue: params.accountId },
{ ParameterKey: 'ReportServiceToken', ParameterValue: params.reportServiceToken },
{ ParameterKey: 'ExternalId', ParameterValue: params.externalId },
{ ParameterKey: 'Version', ParameterValue: params.version },
],
});
// In very rare cases where two deployments to the same org and account happen
// at the exact same time, the `checkIfStackExists` call will fail but then one of the services
// will fail to create the stack since it already exists, so we catch the error
try {
await awsRequest(context, cloudFormationServiceConfig, 'createStack', {
Capabilities: ['CAPABILITY_NAMED_IAM'],
StackName: iamRoleStackName,
TemplateURL: cfnTemplateUrl,
Parameters: [
{ ParameterKey: 'AccountId', ParameterValue: params.accountId },
{ ParameterKey: 'ReportServiceToken', ParameterValue: params.reportServiceToken },
{ ParameterKey: 'ExternalId', ParameterValue: params.externalId },
{ ParameterKey: 'Version', ParameterValue: params.version },
],
});
} catch (err) {
log.notice.success(
'Your AWS account is currently being integrated with Serverless Dashboard'
);
}

if (!(await waitUntilStackIsCreated())) return false;

Expand Down

0 comments on commit bf518b9

Please sign in to comment.