Skip to content

Commit

Permalink
add custom tags to s3-buckets and cloudfront-distributions (#97)
Browse files Browse the repository at this point in the history
* add custom tags to s3-buckets and cloudfront-distributions

* update changelog and version
  • Loading branch information
lissmeister authored Nov 8, 2023
1 parent 0a3c05b commit 1b11da8
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 10 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
## Unreleased
## 2.15.7 (2023-11-08)
### Features
✨ Add custom tags (cost allocation tags) to S3 buckets and CloudFront distribution. This will allow us to see cost per bucket and distributions.

## 2.15.6 (2023-10-30)

Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { set } from "./lib/store.js"
import { checkLisaVersion } from "./lib/versions.js"

export const program = new Command()
export const LISA_VERSION = "2.15.6"
export const LISA_VERSION = "2.15.7"

resetConf()
checkNodeVersion()
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@triggerfishab/lisa-cli",
"version": "2.15.6",
"version": "2.15.7",
"description": "CLI commands to generate a new project based on Lisa",
"main": "./index.js",
"bin": {
Expand Down
57 changes: 50 additions & 7 deletions tasks/services/aws.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
CloudFrontClient,
CreateDistributionCommand,
CreateDistributionWithTagsCommand,
GetDistributionConfigCommand,
ListDistributionsCommand,
UpdateCloudFrontOriginAccessIdentityCommand,
Expand All @@ -18,6 +18,7 @@ import {
GetBucketLocationCommand,
PutBucketLifecycleConfigurationCommand,
PutBucketPolicyCommand,
PutBucketTaggingCommand,
PutBucketVersioningCommand,
PutPublicAccessBlockCommand,
S3Client,
Expand Down Expand Up @@ -63,6 +64,7 @@ export async function setupAWS(environment = "production") {

await putBucketPublicAccessBlock(bucketName)
await putBucketLifeCycleRule(bucketName)
await putBucketTags(bucketName)

writeSuccess(`S3 bucket for ${environment} created.`)

Expand Down Expand Up @@ -118,8 +120,13 @@ export async function setupAWS(environment = "production") {
},
}

const command = new CreateDistributionCommand({
DistributionConfig: distributionConfig,
const command = new CreateDistributionWithTagsCommand({
DistributionConfigWithTags: {
DistributionConfig: distributionConfig,
Tags: {
Items: getTags(bucketName),
},
},
})

const distribution = await cloudFrontClient.send(command)
Expand Down Expand Up @@ -149,7 +156,7 @@ async function putBucketPolicy(bucketName) {
try {
const [accessKeyId, secretAccessKey, canonicalUserId] = await getAWSKeys()

const bucketRegion = await GetBucketRegion(bucketName)
const bucketRegion = await getBucketRegion(bucketName)
// Update bucket policy
const bucketPolicy = {
Version: "2008-10-17",
Expand Down Expand Up @@ -262,7 +269,7 @@ async function putBucketLifeCycleRule(bucketName) {
const [accessKeyId, secretAccessKey, canonicalUserId, accountId] =
await getAWSKeys()

const bucketRegion = await GetBucketRegion(bucketName)
const bucketRegion = await getBucketRegion(bucketName)

const s3Client = new S3Client({
region: bucketRegion,
Expand Down Expand Up @@ -296,12 +303,21 @@ async function putBucketLifeCycleRule(bucketName) {
}
}

function getTags(bucketName) {
return [
{
Key: "cdn.triggerfish.cloud",
Value: bucketName.replace(".cdn.triggerfish.cloud", ""),
},
]
}

async function putBucketPublicAccessBlock(bucketName) {
try {
const [accessKeyId, secretAccessKey, canonicalUserId, accountId] =
await getAWSKeys()

const bucketRegion = await GetBucketRegion(bucketName)
const bucketRegion = await getBucketRegion(bucketName)

const s3Client = new S3Client({
region: bucketRegion,
Expand All @@ -326,7 +342,34 @@ async function putBucketPublicAccessBlock(bucketName) {
}
}

async function GetBucketRegion(bucketName) {
async function putBucketTags(bucketName) {
try {
const [accessKeyId, secretAccessKey, canonicalUserId, accountId] =
await getAWSKeys()

const bucketRegion = await getBucketRegion(bucketName)

const s3Client = new S3Client({
region: bucketRegion,
credentials: { accessKeyId, secretAccessKey },
canonicalUserId,
})

await s3Client.send(
new PutBucketTaggingCommand({
Bucket: bucketName,
ExpectedBucketOwner: accountId,
Tagging: {
TagSet: getTags(bucketName),
},
}),
)
} catch (error) {
writeError(`${bucketName}: ${error}`)
}
}

async function getBucketRegion(bucketName) {
try {
const [accessKeyId, secretAccessKey, canonicalUserId, accountId] =
await getAWSKeys()
Expand Down

0 comments on commit 1b11da8

Please sign in to comment.