From 06bfd8f346ac8f425a7bad78752a8efa63ab3747 Mon Sep 17 00:00:00 2001 From: JHalvor Date: Wed, 16 Oct 2024 13:40:38 +0200 Subject: [PATCH] Exercise-complete --- README.md | 26 +++++++++++-------- .../Controllers/OrderController.cs | 4 +-- WorkshopBackend/OrderService/appsettings.json | 2 +- 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index e08400b..418e5ae 100644 --- a/README.md +++ b/README.md @@ -5,15 +5,15 @@ Note in Every command and URL being called there are fields that need to be repl Example replacements (make sure to replace the curly brackets as well: don't keep curly brackets) -`{studentName}` - example `ajdewilzin` - this can be your name, independent of your login details +`jonashalvorsen` - example `ajdewilzin` - this can be your name, independent of your login details -`{region}` - example `eu-north-1` +`eu-north-1` - example `eu-north-1` ### Steps 1. Create an SNS Topic: ```bash -aws sns create-topic --name {studentName}OrderCreatedTopic +aws sns create-topic --name jonashalvorsenOrderCreatedTopic ``` If successful, you will see in your terminal a JSON response that includes `"TopicArn": "...`. @@ -22,7 +22,7 @@ Replace `_topicArn` in your Controller code with the generated `TopicArn` value 2. Create an SQS Queue: ```bash -aws sqs create-queue --queue-name {studentName}OrderQueue +aws sqs create-queue --queue-name jonashalvorsenOrderQueue ``` If successful, you will see in your terminal a JSON response that includes `"QueueUrl": "some_aws_url`. @@ -31,7 +31,7 @@ Replace `_queueUrl` in your Controller code with the generated `QueueUrl` from t ```bash -aws sns subscribe --topic-arn arn:aws:sns:{region}:637423341661:{studentName}OrderCreatedTopic --protocol sqs --notification-endpoint arn:aws:sqs:{region}:637423341661:{studentName}OrderQueue +aws sns subscribe --topic-arn arn:aws:sns:eu-north-1:637423341661:jonashalvorsenOrderCreatedTopic --protocol sqs --notification-endpoint arn:aws:sqs:eu-north-1:637423341661:jonashalvorsenOrderQueue ``` You don't need to save the generated SubscriptionArn. @@ -39,13 +39,18 @@ You don't need to save the generated SubscriptionArn. 3. Create an EventBridge Event Bus: ```bash -aws events create-event-bus --name {StudentName}CustomEventBus --region {region} +aws events create-event-bus --name jonashalvorsenCustomEventBus --region eu-north-1 ``` 4. Create an EventBridge Rule: ```bash -aws events put-rule --name {StudentName}OrderProcessedRule --event-pattern '{\"source\": [\"order.service\"]}' --event-bus-name {StudentName}CustomEventBus +aws events put-rule --name jonashalvorsenOrderProcessedRule --event-pattern '{\"source\": [\"order.service\"]}' --event-bus-name jonashalvorsenCustomEventBus +``` + +Without \ +```bash +aws events put-rule --name jonashalvorsenOrderProcessedRule --event-pattern '{"source": ["order.service"]}' --event-bus-name jonashalvorsenCustomEventBus ``` If your terminal complains about double quotes, you might need to remove the backslash `\` from the command above (and commands later on). @@ -54,11 +59,11 @@ If your terminal complains about double quotes, you might need to remove the bac 5. Subscribe the SQS Queue to the SNS Topic ```bash -aws sqs get-queue-attributes --queue-url https://sqs.{region}.amazonaws.com/637423341661/{studentName}OrderQueue --attribute-name QueueArn --region {region} +aws sqs get-queue-attributes --queue-url https://sqs.eu-north-1.amazonaws.com/637423341661/jonashalvorsenOrderQueue --attribute-name QueueArn --region eu-north-1 ``` ```bash -aws sns subscribe --topic-arn arn:aws:sns:{region}:637423341661:{studentName}OrderCreatedTopic --protocol sqs --notification-endpoint arn:aws:sqs:{region}:637423341661:{studentName}OrderQueue --region {region} +aws sns subscribe --topic-arn arn:aws:sns:eu-north-1:637423341661:jonashalvorsenOrderCreatedTopic --protocol sqs --notification-endpoint arn:aws:sqs:eu-north-1:637423341661:jonashalvorsenOrderQueue --region eu-north-1 ``` 6. Grant SNS Permissions to SQS @@ -66,10 +71,9 @@ aws sns subscribe --topic-arn arn:aws:sns:{region}:637423341661:{studentName}Ord In Bash/Unix terminals you can run this command: ```bash -aws sqs set-queue-attributes --queue-url https://sqs.{region}.amazonaws.com/637423341661/{studentName}OrderQueue --attributes '{"Policy":"{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"AWS\":\"*\"},\"Action\":\"SQS:SendMessage\",\"Resource\":\"arn:aws:sqs:{region}:637423341661:{studentName}OrderQueue\",\"Condition\":{\"ArnEquals\":{\"aws:SourceArn\":\"arn:aws:sns:{region}:637423341661:{studentName}OrderCreatedTopic\"}}}]}"}' --region {region} +aws sqs set-queue-attributes --queue-url https://sqs.eu-north-1.amazonaws.com/637423341661/jonashalvorsenOrderQueue --attributes '{"Policy":"{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"AWS\":\"*\"},\"Action\":\"SQS:SendMessage\",\"Resource\":\"arn:aws:sqs:eu-north-1:637423341661:jonashalvorsenOrderQueue\",\"Condition\":{\"ArnEquals\":{\"aws:SourceArn\":\"arn:aws:sns:eu-north-1:637423341661:jonashalvorsenOrderCreatedTopic\"}}}]}"}' --region eu-north-1 ``` - ## Core Exercise 1. Create a few orders using a RDS database. Orders to be saved in Database. 2. Update Process flag to false diff --git a/WorkshopBackend/OrderService/Controllers/OrderController.cs b/WorkshopBackend/OrderService/Controllers/OrderController.cs index e49adda..92dd9bf 100644 --- a/WorkshopBackend/OrderService/Controllers/OrderController.cs +++ b/WorkshopBackend/OrderService/Controllers/OrderController.cs @@ -15,8 +15,8 @@ public class OrderController : ControllerBase private readonly IAmazonSQS _sqs; private readonly IAmazonSimpleNotificationService _sns; private readonly IAmazonEventBridge _eventBridge; - private readonly string _queueUrl = ""; // Format of https://.* - private readonly string _topicArn = ""; // Format of arn:aws.* + private readonly string _queueUrl = "https://sqs.eu-north-1.amazonaws.com/637423341661/jonashalvorsenOrderQueue"; // Format of https://.* + private readonly string _topicArn = "arn:aws:sns:eu-north-1:637423341661:jonashalvorsenOrderCreatedTopic"; // Format of arn:aws.* public OrderController() { diff --git a/WorkshopBackend/OrderService/appsettings.json b/WorkshopBackend/OrderService/appsettings.json index 1c233fe..823fb54 100644 --- a/WorkshopBackend/OrderService/appsettings.json +++ b/WorkshopBackend/OrderService/appsettings.json @@ -10,4 +10,4 @@ } }, "AllowedHosts": "*" -} +} \ No newline at end of file