forked from ecamp/ecamp3
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
aws-setup: add aws stack with bucket and a write only and a read only…
… user Issue: ecamp#3478
- Loading branch information
Showing
1 changed file
with
115 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |