Skip to content

Commit 48ca4ff

Browse files
authored
Merge pull request #378 from jdno/deploy-sqs
Deploy SQS queue for crates.io to staging
2 parents bbab800 + a938704 commit 48ca4ff

File tree

6 files changed

+114
-22
lines changed

6 files changed

+114
-22
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"aws": {
3+
"profile": "crates-io-staging",
4+
"region": "us-east-2"
5+
}
6+
}

terragrunt/accounts/crates-io-staging/crates-io-logs/.terraform.lock.hcl

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
terraform {
2+
source = "../../../..//terragrunt/modules/crates-io-logs"
3+
}
4+
5+
include {
6+
path = find_in_parent_folders()
7+
merge_strategy = "deep"
8+
}
9+
10+
inputs = {
11+
bucket_account = 890664054962
12+
bucket_arn = "arn:aws:s3:::rust-staging-crates-io-logs"
13+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Infrastructure to Count Crate Downloads
2+
3+
This module creates the infrastructure that enables [crates.io] to count crate
4+
downloads asynchronously using request logs from our Content Delivery Networks.
5+
6+
Whenever a new archive with request logs is uploaded to S3, S3 pushes an event
7+
into a SQS queue. [crates.io] monitors the queue and processes incoming events.
8+
From the event, it can determine what files to fetch from S3, download and then
9+
parse them, and update the download counts in the database.
10+
11+
```mermaid
12+
sequenceDiagram
13+
static.crates.io ->> S3: Uploads logs
14+
S3 ->> SQS: Queues event
15+
crates.io ->> SQS: Pulls event from queue
16+
crates.io ->> S3: Fetches new log file
17+
crates.io ->> crates.io: Parses log file
18+
crates.io ->> crates.io: Updates download counts
19+
```
20+
21+
See [rust-lang/simpleinfra#372] for a detailed discussion of the design.
22+
23+
## AWS Accounts
24+
25+
The infrastructure for [crates.io] has historically been deployed to the
26+
`legacy` AWS account. For this infrastructure, new accounts have been created
27+
that follow the new convention of specialized and isolated accounts for
28+
services.
29+
30+
This requires the S3 bucket with the request logs in the `legacy` account to
31+
push events into the SQS queue in a different account. And the [crates.io]
32+
application needs a second set of AWS credentials to pull events from the
33+
queue.
34+
35+
[crates.io]: https://crates.io
36+
[rust-lang/simpleinfra#372]: https://github.com/rust-lang/simpleinfra/issues/372
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
terraform {
2+
required_version = "~> 1"
3+
4+
required_providers {
5+
aws = {
6+
source = "hashicorp/aws"
7+
version = "~> 4.32"
8+
}
9+
}
10+
}
11+
12+
variable "bucket_account" {
13+
type = number
14+
description = "Account ID of the S3 bucket which will send events to the SQS queue"
15+
}
16+
17+
variable "bucket_arn" {
18+
type = string
19+
description = "ARN of the S3 bucket which will send events to the SQS queue"
20+
}
Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
resource "aws_sqs_queue" "log_event_queue" {
2-
name = "cdn-log-queue"
1+
resource "aws_sqs_queue" "cdn_log_event_queue" {
2+
name = "cdn-log-event-queue"
33
receive_wait_time_seconds = 20
44
}
55

66
resource "aws_sqs_queue_policy" "s3_push" {
7-
queue_url = aws_sqs_queue.log_event_queue.id
7+
queue_url = aws_sqs_queue.cdn_log_event_queue.id
88
policy = data.aws_iam_policy_document.s3_push_to_queue.json
99
}
1010

1111
data "aws_iam_policy_document" "s3_push_to_queue" {
1212
statement {
13-
sid = "allow-s3-to-push-events"
13+
sid = "AllowS3ToPushEvents"
1414
effect = "Allow"
1515
principals {
1616
type = "Service"
@@ -19,45 +19,37 @@ data "aws_iam_policy_document" "s3_push_to_queue" {
1919

2020
actions = ["sqs:SendMessage"]
2121

22-
resources = [aws_sqs_queue.log_event_queue.arn]
22+
resources = [aws_sqs_queue.cdn_log_event_queue.arn]
2323
condition {
2424
test = "ArnLike"
2525
variable = "aws:SourceArn"
26-
values = [data.aws_arn.src_bucket.arn]
26+
values = [var.bucket_arn]
2727
}
2828
condition {
2929
test = "StringEquals"
3030
variable = "aws:SourceAccount"
31-
values = [data.aws_arn.src_bucket.account]
31+
values = [var.bucket_account]
3232
}
3333
}
3434
}
3535

36-
data "aws_arn" "src_bucket" {
37-
arn = var.src_log_bucket_arn
38-
}
39-
40-
variable "src_log_bucket_arn" {
41-
type = string
42-
description = "Bucket ARN which will send events to the SQS queue"
43-
}
44-
4536
resource "aws_iam_user" "heroku_access" {
4637
name = "crates-io-heroku-access"
4738
}
4839

4940
resource "aws_iam_access_key" "crates_io" {
50-
user = aws_iam_user.heroku_access
41+
user = aws_iam_user.heroku_access.name
5142
}
5243

53-
resouce "aws_iam_user_policy" "sqs_read" {
54-
name = "heroku-access"
55-
user = aws_iam_user.heroku_access.name
44+
resource "aws_iam_user_policy" "sqs_read" {
45+
name = "heroku-access"
46+
user = aws_iam_user.heroku_access.name
47+
policy = data.aws_iam_policy_document.heroku_access.json
5648
}
5749

5850
data "aws_iam_policy_document" "heroku_access" {
5951
statement {
60-
sid = "allow-sqs"
52+
sid = "AllowAccessToSQS"
6153
effect = "Allow"
6254

6355
actions = [
@@ -67,6 +59,6 @@ data "aws_iam_policy_document" "heroku_access" {
6759
"sqs:ReceiveMessage",
6860
]
6961

70-
resources = [aws_sqs_queue.log_event_queue.arn]
62+
resources = [aws_sqs_queue.cdn_log_event_queue.arn]
7163
}
7264
}

0 commit comments

Comments
 (0)