Skip to content

Commit cf88f15

Browse files
Convert iam-policy-stream
1 parent c59924e commit cf88f15

1 file changed

Lines changed: 40 additions & 28 deletions

File tree

src/content/docs/aws/tutorials/iam-policy-stream.mdx

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ When you're developing cloud and serverless applications, you need to grant acce
1919
To handle this, you create IAM roles and assign permissions through policies.
2020
However, configuring these policies can be challenging, especially if you want to ensure minimal access of all principals to your resources.
2121

22-
[LocalStack IAM Policy Stream](https://app.localstack.cloud/policy-stream) automates the generation of IAM policies for your AWS API requests on your local machine.
22+
[LocalStack IAM Policy Stream](https://app.localstack.cloud/inst/default/policy-stream) automates the generation of IAM policies for your AWS API requests on your local machine.
2323
This stream helps you identify the necessary permissions for your cloud application and allows you to detect logical errors, such as unexpected actions in your policies.
2424

2525
This tutorial will guide you through setting up IAM Policy Stream for a locally running AWS application.
@@ -41,10 +41,10 @@ Additionally, it serves as a useful learning tool, helping users understand the
4141

4242
## Prerequisites
4343

44-
- [LocalStack CLI](/aws/getting-started/installation) with [`LOCALSTACK_AUTH_TOKEN`](/aws/getting-started/auth-token)
44+
- [`lstk`](/aws/getting-started/installation#lstk) with [`LOCALSTACK_AUTH_TOKEN`](/aws/getting-started/auth-token)
4545
- [Docker](https://docs.docker.com/get-docker/)
46-
- [Terraform](https://developer.hashicorp.com/terraform/install) & [`tflocal` wrapper](https://github.com/localstack/terraform-local)
47-
- [AWS](https://docs.aws.amazon.com/cli/v1/userguide/cli-chap-install.html) CLI with [`awslocal` wrapper](https://github.com/localstack/awscli-local)
46+
- [Terraform](https://developer.hashicorp.com/terraform/install) & [`lstk terraform`](/aws/connecting/infrastructure-as-code/terraform#lstk-terraform)
47+
- [AWS](https://docs.aws.amazon.com/cli/v1/userguide/cli-chap-install.html) CLI with [`lstk aws`](/aws/connecting/aws-cli#localstack-aws-cli-lstk-aws)
4848
- [LocalStack account](https://www.localstack.cloud/pricing)
4949
- [`jq`](https://jqlang.github.io/jq/download/)
5050

@@ -74,13 +74,13 @@ With LocalStack's IAM enforcement enabled, you can thoroughly test your policy a
7474
Launch the LocalStack container on your local machine using the specified command:
7575

7676
```bash
77-
DEBUG=1 IAM_SOFT_MODE=1 localstack start
77+
LOCALSTACK_DEBUG=1 LOCALSTACK_IAM_SOFT_MODE=1 lstk start
7878
```
7979

8080
In the above command:
8181

82-
- `DEBUG=1` turns on detailed logging to check API calls and IAM violations.
83-
- `IAM_SOFT_MODE=1` lets you test IAM enforcement by logging violations without stopping the API calls.
82+
- `LOCALSTACK_DEBUG=1` turns on detailed logging to check API calls and IAM violations.
83+
- `LOCALSTACK_IAM_SOFT_MODE=1` lets you test IAM enforcement by logging violations without stopping the API calls.
8484

8585
### Create the Terraform configuration
8686

@@ -116,12 +116,12 @@ resource "aws_s3_bucket_notification" "bucket_notification" {
116116

117117
### Deploy the Terraform configuration
118118

119-
You can use `tflocal` to deploy your Terraform configuration within the LocalStack environment.
119+
You can use `lstk terraform` to deploy your Terraform configuration within the LocalStack environment.
120120
Run the following commands to initialize and apply the Terraform configuration:
121121

122122
```bash
123-
tflocal init
124-
tflocal apply
123+
lstk terraform init
124+
lstk terraform apply
125125
```
126126

127127
You will be prompted to confirm the changes.
@@ -145,7 +145,7 @@ Apply complete! Resources: 3 added, 0 changed, 0 destroyed.
145145

146146
### Start the IAM Policy Stream
147147

148-
Access the [LocalStack Web Application](https://app.localstack.cloud/) and go to the [IAM Policy Stream dashboard](https://app.localstack.cloud/policy-stream).
148+
Access the [LocalStack Web Application](https://app.localstack.cloud/) and go to the [IAM Policy Stream dashboard](https://app.localstack.cloud/inst/default/policy-stream).
149149
This feature enables you to directly examine the generated policies, displaying the precise permissions required for each API call.
150150

151151
![IAM Policy Stream dashboard](/images/aws/iam-policy-stream-dashboard.png)
@@ -159,11 +159,11 @@ Create a new file named `some-log-file.log` and upload it to the S3 bucket using
159159

160160
```bash
161161
echo "Hello, LocalStack" > some-log-file.log
162-
awslocal s3 cp some-log-file.log s3://s3-event-notification-bucket/
162+
lstk aws s3 cp some-log-file.log s3://s3-event-notification-bucket/
163163
```
164164

165165
Uploading a file will activate an event notification, sending a message to the SQS queue.
166-
However, since the SQS queue lacks the necessary permissions, an IAM violation will appear in the [IAM Policy Stream dashboard](https://app.localstack.cloud/policy-stream).
166+
However, since the SQS queue lacks the necessary permissions, an IAM violation will appear in the [IAM Policy Stream dashboard](https://app.localstack.cloud/inst/default/policy-stream).
167167

168168
![IAM Policy Stream showcasing an IAM violation](/images/aws/iam-policy-stream-violation.png)
169169

@@ -221,14 +221,14 @@ POLICY
221221
Now, re-apply the Terraform configuration to update the SQS queue with the new policy:
222222

223223
```bash
224-
tflocal apply
224+
lstk terraform apply
225225
```
226226

227227
Next, trigger the event notification again by uploading a file to the S3 bucket.
228228
You can confirm that the S3 bucket is correctly set up for event notifications through the SQS queue by checking if the message is received in the SQS queue:
229229

230230
```bash
231-
awslocal sqs receive-message \
231+
lstk aws sqs receive-message \
232232
--queue-url http://sqs.us-east-1.localhost.localstack.cloud:4566/000000000000/s3-event-notification-queue
233233
```
234234

@@ -273,13 +273,13 @@ This section demonstrates how to test your IAM policies and verify both deny and
273273

274274
### Testing Scenario 1: Deny (Without IAM Policy)
275275

276-
When you first upload a file to the S3 bucket without the proper SQS queue policy in place, the S3 service will be denied permission to send messages to the SQS queue.
276+
When you first upload a file to the S3 bucket without the proper SQS queue policy in place, S3 has no permission to send messages to the SQS queue, and the violation is reported.
277277

278278
**Upload a test file:**
279279

280280
```bash
281281
echo "Test file content" > test-file.log
282-
awslocal s3 cp test-file.log s3://s3-event-notification-bucket/
282+
lstk aws s3 cp test-file.log s3://s3-event-notification-bucket/
283283
```
284284

285285
**Expected output - IAM Violation in LocalStack logs:**
@@ -305,19 +305,28 @@ The dashboard clearly shows:
305305
**Attempting to receive messages from the queue:**
306306

307307
```bash
308-
awslocal sqs receive-message \
308+
lstk aws sqs receive-message \
309309
--queue-url http://sqs.us-east-1.localhost.localstack.cloud:4566/000000000000/s3-event-notification-queue
310310
```
311311

312-
**Expected output - No messages (because S3 was denied):**
312+
**Expected output - the message is still delivered, because soft mode does not block the call:**
313313

314314
```json
315315
{
316-
"Messages": []
316+
"Messages": [
317+
{
318+
"MessageId": "5da627c5-5b4b-4202-a499-510222727e43",
319+
"ReceiptHandle": "NWMyZDA1MDEtM2NlYi00MzBjLWIyNjQtYjM2ZjNmYmQxZTAyIGFybjphd3M6c3FzOnVzLWVhc3QtMTowMDAwMDAwMDAwMDA6czMtZXZlbnQtbm90aWZpY2F0aW9uLXF1ZXVlIDVkYTYyN2M1LTViNGItNDIwMi1hNDk5LTUxMDIyMjcyN2U0MyAxNzg2NjY1NzQ3LjQ3ODI1MTI=",
320+
"MD5OfBody": "dee5cf145a0678a0ac02e3b38aa302f7",
321+
"Body": "{\"Records\": [{\"eventVersion\": \"2.1\", \"eventSource\": \"aws:s3\", \"awsRegion\": \"us-east-1\", \"eventName\": \"ObjectCreated:Put\", \"s3\": {\"bucket\": {\"name\": \"s3-event-notification-bucket\"}, \"object\": {\"key\": \"test-file.log\", \"size\": 18}}}]}"
322+
}
323+
]
317324
}
318325
```
319326

320-
Or you may receive no output at all, indicating an empty queue.
327+
:::note
328+
In a production environment, where the same policy gap would be enforced by real AWS IAM, this `SendMessage` call would fail and the message would never arrive.
329+
:::
321330

322331
### Testing Scenario 2: Allow (With IAM Policy)
323332

@@ -353,7 +362,7 @@ After applying the IAM policy generated by the Policy Stream to your SQS queue,
353362

354363
```bash
355364
echo "Test file with policy" > test-file-2.log
356-
awslocal s3 cp test-file-2.log s3://s3-event-notification-bucket/
365+
lstk aws s3 cp test-file-2.log s3://s3-event-notification-bucket/
357366
```
358367

359368
**Expected output - Success (no IAM violation):**
@@ -362,13 +371,16 @@ awslocal s3 cp test-file-2.log s3://s3-event-notification-bucket/
362371
upload: ./test-file-2.log to s3://s3-event-notification-bucket/test-file-2.log
363372
```
364373

365-
**LocalStack logs showing successful permission:**
374+
**LocalStack logs:**
366375

367-
```shell
368-
2024-07-09T05:35:22.123 DEBUG --- [et.reactor-2] l.s.i.p.handler : Request for service 'sqs' by principal 's3.amazonaws.com' for operation 'SendMessage' allowed.
369-
2024-07-09T05:35:22.123 DEBUG --- [et.reactor-2] l.s.i.p.handler : 1 permissions have been explicitly allowed: ["Action 'sqs:SendMessage' for 'arn:aws:sqs:us-east-1:000000000000:s3-event-notification-queue'"]
376+
An allowed request produces no output from the IAM policy handler — only denials are logged.
377+
378+
```bash
379+
lstk logs | grep "i.p.handler"
370380
```
371381

382+
Only the entries from the earlier, un-permitted upload should remain.
383+
372384
**IAM Policy Stream Dashboard showing no violations:**
373385

374386
![IAM Policy Stream showcasing no violations](/images/aws/iam-policy-stream-no-violations.png)
@@ -378,7 +390,7 @@ The dashboard shows all actions with green checkmarks, indicating successful exe
378390
**Receive the message from the queue:**
379391

380392
```bash
381-
awslocal sqs receive-message \
393+
lstk aws sqs receive-message \
382394
--queue-url http://sqs.us-east-1.localhost.localstack.cloud:4566/000000000000/s3-event-notification-queue
383395
```
384396

@@ -405,7 +417,7 @@ To ensure your IAM policies are correctly configured:
405417

406418
- **No IAM violations** appear in the IAM Policy Stream dashboard
407419
- **Messages are successfully delivered** to the SQS queue
408-
- **LocalStack logs show "allowed"** for the `SendMessage` operation
420+
- **No new violation entries** are logged for the `SendMessage` operation after the policy is applied
409421
- **All API calls display green checkmarks** in the Policy Stream dashboard
410422

411423
## Conclusion

0 commit comments

Comments
 (0)