Skip to content
This repository has been archived by the owner on Feb 3, 2021. It is now read-only.

Commit

Permalink
feat: allow to upload avatars
Browse files Browse the repository at this point in the history
  • Loading branch information
coderbyheart committed Jul 29, 2019
1 parent fe941fd commit 6e37c97
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
38 changes: 38 additions & 0 deletions cdk/resources/AvatarStorage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import * as CloudFormation from '@aws-cdk/core'
import * as S3 from '@aws-cdk/aws-s3'
import * as IAM from '@aws-cdk/aws-iam'

/**
* Storage for avatars
*/
export class AvatarStorage extends CloudFormation.Resource {
public readonly bucket: S3.IBucket
public constructor(
parent: CloudFormation.Stack,
id: string,
{ userRole }: { userRole: IAM.Role },
) {
super(parent, id)

this.bucket = new S3.Bucket(this, 'bucket', {
publicReadAccess: true,
cors: [
{
allowedHeaders: ['*'],
allowedMethods: [S3.HttpMethods.GET, S3.HttpMethods.PUT],
allowedOrigins: ['*'],
exposedHeaders: ['Date'],
maxAge: 3600,
},
],
removalPolicy: CloudFormation.RemovalPolicy.DESTROY,
})

userRole.addToPolicy(
new IAM.PolicyStatement({
resources: [`${this.bucket.bucketArn}/*`],
actions: ['s3:PutObject'],
}),
)
}
}
15 changes: 14 additions & 1 deletion cdk/stacks/Bifravst.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { BifravstLambdas } from '../cloudformation'
import { LayeredLambdas } from '@nrfcloud/package-layered-lambdas'
import { WebAppHosting } from '../resources/WebAppHosting'
import { RepublishDesiredConfig } from '../resources/RepublishDesiredConfig'
import { AvatarStorage } from '../resources/AvatarStorage'

export class BifravstStack extends CloudFormation.Stack {
public constructor(
Expand Down Expand Up @@ -85,7 +86,11 @@ export class BifravstStack extends CloudFormation.Stack {
statements: [
new IAM.PolicyStatement({
resources: ['*'],
actions: ['iot:listThings', 'iot:describeThing'],
actions: [
'iot:listThings',
'iot:describeThing',
'iot:updateThing',
],
}),
],
}),
Expand Down Expand Up @@ -345,6 +350,13 @@ export class BifravstStack extends CloudFormation.Stack {
})

new RepublishDesiredConfig(this, 'republishDesiredConfig')

const avatarStorage = new AvatarStorage(this, 'avatars', { userRole })

new CloudFormation.CfnOutput(this, 'avatarBucketName', {
value: avatarStorage.bucket.bucketName,
exportName: `${this.stackName}:avatarBucketName`,
})
}
}

Expand All @@ -363,4 +375,5 @@ export type StackOutputs = {
thingPolicyArn: string
thingGroupName: string
userIotPolicyArn: string
avatarBucket: string
}

0 comments on commit 6e37c97

Please sign in to comment.