Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: cleanup stale resources in CodeBuild #12779

Merged
merged 28 commits into from
Jun 21, 2023

Conversation

jhockett
Copy link
Contributor

Description of changes

Add a CodeBuild job similar to what we have for CircleCI to cleanup resources that were not deleted properly during test runs.

Description of how you validated changes

Ran job on my local copy of the CodeBuild infrastructure, cleanup job executes and passes.

Checklist

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@jhockett jhockett requested review from a team as code owners June 12, 2023 16:34
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file is mostly a rename of packages/amplify-e2e-core/src/utils/add-circleci-tags.ts, but with a few changes to switch the tag data source depending on the CI environment.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a rough copy of packages/amplify-e2e-tests/src/cleanup-e2e-resources.ts modified to work for CodeBuild.

kuhlmanp
kuhlmanp previously approved these changes Jun 16, 2023
codebuild_specs/cleanup_resources.yml Show resolved Hide resolved
*/
const handleExpiredTokenException = (): void => {
console.log('Token expired. Exiting...');
process.exit();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.exit(1)?

if (typeof created === 'string') {
normalizedDate = new Date(created);
} else {
normalizedDate = created;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this cause problems if created is undefined?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so, since the later ternary will only call .getTime() if normalizedDate is defined

* Get all iam roles in the account, and filter down to the ones we consider stale.
*/
const getOrphanTestIamRoles = async (account: AWSAccountInfo): Promise<IamRoleInfo[]> => {
const iamClient = new IAM(getAWSConfig(account));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we use the sdk global config object to avoid having to get the config in all these places?
Alternatively, create the clients we need in the module scope and use them when needed in the functions

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might be missing something, as I didn't implement the original script (cc @lazpavel), but I don't like that as much because we will have to update the global config for each account. Introducing global state makes the implementation less flexible and reliant on side effects imho.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yeah if we are switching accounts as the script runs then this is okay

};

const ci = CI.toLowerCase();
addTagIfNotExist(ci, sanitizeTagValue(CI === CIRCLECI ? process.env[CI] : process.env[`${CODEBUILD}_BUILD_IMAGE`]));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit, this could be simplified into a for loop like:

[[key1, val1], [key2, val2], ...].forEach(([key, value]) => addTagIfNotExists(key, sanitizeTagValue(value))

Comment on lines 18 to 19
// Ensure that scripts/cci-utils.ts is also updated when this gets updated
const AWS_REGIONS_TO_RUN_TESTS = [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we just import the region list from that file?

edwardfoyle
edwardfoyle previously approved these changes Jun 21, 2023
Copy link
Member

@edwardfoyle edwardfoyle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One CodeQL issue but LGTM

Copy link
Member

@edwardfoyle edwardfoyle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, it looks like the AWS_REGIONS_TO_RUN_TESTS import didn't get updated?

@edwardfoyle edwardfoyle self-requested a review June 21, 2023 16:34
@jhockett
Copy link
Contributor Author

One CodeQL issue but LGTM

Shoot, I included a commit by mistake which triggered the CodeQL warning. Updating...

Actually, it looks like the AWS_REGIONS_TO_RUN_TESTS import didn't get updated?

It did, but only in the CodeBuild cleanup script. The old CCI cleanup script lives in the amplify-e2e-tests package, so I left it as is so we don't import from the scripts folder.

@codecov-commenter
Copy link

Codecov Report

Merging #12779 (daf497b) into dev (ec9a2ba) will increase coverage by 48.43%.
The diff coverage is 57.14%.

❗ Your organization is not using the GitHub App Integration. As a result you may experience degraded service beginning May 15th. Please install the Github App Integration for your organization. Read more.

@@             Coverage Diff             @@
##              dev   #12779       +/-   ##
===========================================
+ Coverage    0.00%   48.43%   +48.43%     
===========================================
  Files        1296      841      -455     
  Lines      149743    38076   -111667     
  Branches     1296     7752     +6456     
===========================================
+ Hits            0    18441    +18441     
+ Misses     148447    18045   -130402     
- Partials     1296     1590      +294     
Impacted Files Coverage Δ
...ify-category-function/src/events/prePushHandler.ts 33.33% <0.00%> (+33.33%) ⬆️
...ider-utils/awscloudformation/utils/layerHelpers.ts 21.80% <0.00%> (+21.80%) ⬆️
...ib/S3AndCloudFront/helpers/configure-CloudFront.js 87.06% <ø> (+87.06%) ⬆️
...lify-category-hosting/lib/S3AndCloudFront/index.js 89.65% <ø> (+89.65%) ⬆️
...e/src/amplify-node-pkg-detector/lock-file-types.ts 100.00% <ø> (+100.00%) ⬆️
.../src/amplify-node-pkg-detector/yarn-lock-parser.ts 100.00% <ø> (+100.00%) ⬆️
packages/amplify-cli-core/src/constants.ts 100.00% <ø> (+100.00%) ⬆️
...es/amplify-cli-core/src/context/plugin-platform.ts 33.33% <0.00%> (+33.33%) ⬆️
...s/amplify-cli-core/src/errors/amplify-exception.ts 82.35% <ø> (+82.35%) ⬆️
...ackages/amplify-cli-core/src/help/commands-info.ts 100.00% <ø> (+100.00%) ⬆️
... and 33 more

... and 1261 files with indirect coverage changes

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@jhockett jhockett merged commit de98654 into aws-amplify:dev Jun 21, 2023
19 checks passed
@jhockett jhockett deleted the cb-resource-cleanup branch June 21, 2023 20:42
@jhockett jhockett restored the cb-resource-cleanup branch June 22, 2023 19:27
jhockett added a commit that referenced this pull request Jun 22, 2023
jhockett added a commit that referenced this pull request Jun 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants