diff --git a/code/media-query/final/.chalice/config.json b/code/media-query/final/.chalice/config.json index cdbc4e0..4f6205b 100644 --- a/code/media-query/final/.chalice/config.json +++ b/code/media-query/final/.chalice/config.json @@ -4,7 +4,14 @@ "stages": { "dev": { "api_gateway_stage": "api", - "autogen_policy": false + "autogen_policy": false, + "environment_variables": { + "MEDIA_BUCKET_NAME": "${aws_s3_bucket.media_bucket.bucket}", + "MEDIA_TABLE_NAME": "${aws_dynamodb_table.media_table.name}", + "VIDEO_TOPIC_ARN": "${aws_sns_topic.video_topic.arn}", + "VIDEO_TOPIC_NAME": "${aws_sns_topic.video_topic.name}", + "VIDEO_ROLE_ARN": "${aws_iam_role.media_role.arn}" + } } } } diff --git a/code/media-query/final/recordresources.py b/code/media-query/final/recordresources.py deleted file mode 100644 index fbf9b8e..0000000 --- a/code/media-query/final/recordresources.py +++ /dev/null @@ -1,41 +0,0 @@ -import argparse -import json -import os - -import boto3 -from botocore import xform_name - - -def record_as_env_var(stack_name, stage): - cloudformation = boto3.client('cloudformation') - response = cloudformation.describe_stacks( - StackName=stack_name - ) - outputs = response['Stacks'][0]['Outputs'] - with open(os.path.join('.chalice', 'config.json')) as f: - data = json.load(f) - data['stages'].setdefault(stage, {}).setdefault( - 'environment_variables', {} - ) - for output in outputs: - data['stages'][stage]['environment_variables'][ - _to_env_var_name(output['OutputKey'])] = output['OutputValue'] - with open(os.path.join('.chalice', 'config.json'), 'w') as f: - serialized = json.dumps(data, indent=2, separators=(',', ': ')) - f.write(serialized + '\n') - - -def _to_env_var_name(name): - return xform_name(name).upper() - - -def main(): - parser = argparse.ArgumentParser() - parser.add_argument('-s', '--stage', default='dev') - parser.add_argument('--stack-name', required=True) - args = parser.parse_args() - record_as_env_var(stack_name=args.stack_name, stage=args.stage) - - -if __name__ == '__main__': - main() diff --git a/code/media-query/final/resources.json b/code/media-query/final/resources.json deleted file mode 100644 index 3349bb8..0000000 --- a/code/media-query/final/resources.json +++ /dev/null @@ -1,104 +0,0 @@ -{ - "Outputs": { - "MediaBucketName": { - "Value": { - "Ref": "MediaBucket" - } - }, - "MediaTableName": { - "Value": { - "Ref": "MediaTable" - } - }, - "VideoTopicArn": { - "Value": { - "Ref": "VideoTopic" - } - }, - "VideoTopicName": { - "Value": { - "Fn::GetAtt": [ - "VideoTopic", - "TopicName" - ] - } - }, - "VideoRoleArn": { - "Value": { - "Fn::GetAtt": [ - "VideoRole", - "Arn" - ] - } - } - }, - "Resources": { - "MediaBucket": { - "Type": "AWS::S3::Bucket" - }, - "MediaTable": { - "Properties": { - "AttributeDefinitions": [ - { - "AttributeName": "name", - "AttributeType": "S" - } - ], - "KeySchema": [ - { - "AttributeName": "name", - "KeyType": "HASH" - } - ], - "ProvisionedThroughput": { - "ReadCapacityUnits": 5, - "WriteCapacityUnits": 5 - } - }, - "Type": "AWS::DynamoDB::Table" - }, - "VideoTopic": { - "Type": "AWS::SNS::Topic" - }, - "VideoRole": { - "Properties": { - "AssumeRolePolicyDocument": { - "Statement": [ - { - "Effect": "Allow", - "Action": [ - "sts:AssumeRole" - ], - "Principal": { - "Service": [ - "rekognition.amazonaws.com" - ] - } - } - ] - }, - "Policies": [ - { - "PolicyName": "RekognitionPublish", - "PolicyDocument": { - "Statement": [ - { - "Effect": "Allow", - "Action": [ - "sns:Publish" - ], - "Resource": [ - { - "Ref": "VideoTopic" - } - ] - } - ] - } - } - ] - }, - "Type": "AWS::IAM::Role" - } - } -} diff --git a/code/media-query/final/terraform/media-resources.tf b/code/media-query/final/terraform/media-resources.tf new file mode 100644 index 0000000..699b917 --- /dev/null +++ b/code/media-query/final/terraform/media-resources.tf @@ -0,0 +1,64 @@ +resource "random_id" "resource_names" { + byte_length = 8 + prefix = "media-query-" +} + + +resource "aws_s3_bucket" "media_bucket" { + bucket_prefix = "media-query" +} + +resource "aws_dynamodb_table" "media_table" { + name = "${random_id.resource_names.dec}" + hash_key = "name" + attribute { + name = "name" + type = "S" + } + read_capacity = 5 + write_capacity = 5 +} + + +resource "aws_sns_topic" "video_topic" { +} + +resource "aws_iam_role_policy" "media_policy" { + name = "media_policy" + role = "${aws_iam_role.media_role.id}" + + policy = "${data.aws_iam_policy_document.allow_publish.json}" +} + +resource "aws_iam_role" "media_role" { + name_prefix = "media-query-" + + assume_role_policy = <