Skip to content

Commit

Permalink
aws-setup: add aws stack with bucket and a write only and a read only…
Browse files Browse the repository at this point in the history
… user

Issue: ecamp#3478
  • Loading branch information
BacLuc committed Jul 31, 2023
1 parent 8c3584a commit c04f1b6
Showing 1 changed file with 115 additions and 7 deletions.
122 changes: 115 additions & 7 deletions aws-setup/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,117 @@
import * as pulumi from '@pulumi/pulumi'
import * as aws from '@pulumi/aws'
import * as awsx from '@pulumi/awsx'
import { AccessKey, Policy, User, UserPolicyAttachment } from '@pulumi/aws/iam'
import { Bucket } from '@pulumi/aws/s3'
import { Config } from '@pulumi/pulumi'

// Create an AWS resource (S3 Bucket)
const bucket = new aws.s3.Bucket('my-bucket')
const config = new Config()
const environment = config.require('env') || 'dev'

// Export the name of the bucket
export const bucketName = bucket.id
const putObjectPolicy = new Policy('put-object', {
policy: {
Version: '2012-10-17',
Statement: [
{
Effect: 'Allow',
Action: 's3:PutObject',
Resource: '*',
},
],
},
})

const putOnlyUser = new User(`ecamp3-${environment}-put-only-user`, {
name: `ecamp3-${environment}-put-only-user`,
permissionsBoundary: putObjectPolicy.arn,
})

const putOnlyUserAccessKey = new AccessKey(
`ecamp3-${environment}-put-only-user-access-key`,
{
user: putOnlyUser.name,
}
)

const downloadObjectPolicy = new Policy('download-object', {
policy: {
Version: '2012-10-17',
Statement: [
{
Effect: 'Allow',
Action: ['s3:GetObjectVersion', 's3:ListBucket', 's3:ListBucketVersions'],
Resource: '*',
},
],
},
})

const downloadOnlyUser = new User(`ecamp3-${environment}-download-only-user`, {
name: `ecamp3-${environment}-download-only-user`,
permissionsBoundary: downloadObjectPolicy.arn,
})

const downloadOnlyAccessKey = new AccessKey(
`ecamp3-${environment}-download-only-user-access-key`,
{
user: downloadOnlyUser.name,
}
)

const retentionPolicies = {
transitions: [
{
days: 30,
storageClass: 'GLACIER',
},
],
expiration: {
days: 365,
},
}

let objectLockRetentionDays = 365
if (environment === 'dev') {
retentionPolicies.transitions[0].days = 1
retentionPolicies.expiration.days = 7
objectLockRetentionDays = 8
}

const backupBucket = new Bucket(`ecamp3-${environment}-bucket`, {
acl: 'private',
versioning: {
enabled: true,
},
lifecycleRules: [
{
enabled: true,
abortIncompleteMultipartUploadDays: 1,
...retentionPolicies,
},
],
objectLockConfiguration: {
objectLockEnabled: 'Enabled',
rule: {
defaultRetention: {
mode: 'GOVERNANCE',
days: objectLockRetentionDays,
},
},
},
})

new UserPolicyAttachment(`ecamp3-${environment}-put-only-policy-attachment`, {
user: putOnlyUser.name,
policyArn: putObjectPolicy.arn,
})

new UserPolicyAttachment(`ecamp3-${environment}-download-only-policy-attachment`, {
user: downloadOnlyUser.name,
policyArn: downloadObjectPolicy.arn,
})

export const bucketEndpoint = backupBucket.bucketDomainName
export const bucketName = backupBucket.bucket

export const putOnlyUserAccessKeyId = putOnlyUserAccessKey.id
export const putOnlyUserSecretAccessKey = putOnlyUserAccessKey.secret

export const downloadOnlyUserAccessKeyId = downloadOnlyAccessKey.id
export const downloadOnlyUserSecretAccessKey = downloadOnlyAccessKey.secret

0 comments on commit c04f1b6

Please sign in to comment.