You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/content/docs/aws/tutorials/s3-static-website-terraform.mdx
+37-40Lines changed: 37 additions & 40 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -53,10 +53,15 @@ In this architecture:
53
53
We will create a simple static website using plain HTML to get started.
54
54
To create a static website deployed over S3, we need to create an index document and a custom error document.
55
55
We will name our index document `index.html` and our error document `error.html`.
56
-
Optionally, you can create a folder called `assets` to store images and other assets.
57
56
58
-
Let's create a directory named `s3-static-website-localstack` where we'll store our static website files.
59
-
If you don't have an `index.html` file, you can use the following code to create one:
57
+
Let's create a directory named `s3-static-website-localstack` where we'll store our project files, with a `www/` subdirectory that holds the website content:
58
+
59
+
```bash
60
+
mkdir -p s3-static-website-localstack/www
61
+
cd s3-static-website-localstack
62
+
```
63
+
64
+
Inside `www/`, create an `index.html` file with the following content:
60
65
61
66
```html showLineNumbers
62
67
<!DOCTYPE html>
@@ -74,7 +79,7 @@ If you don't have an `index.html` file, you can use the following code to create
74
79
75
80
S3 will serve this file when a user visits the root URL of your static website, serving as the default page.
76
81
In a similar fashion, you can configure a custom error document that contains a user-friendly error message.
77
-
Let's create a file named `error.html` and add the following code:
82
+
Create a file named `error.html` next to `index.html` inside `www/` and add the following code:
@@ -197,6 +203,11 @@ We also publish an SSL certificate which is automatically used inside LocalStack
197
203
For most of the other services, it is fine to use `localhost:4566`.
198
204
:::
199
205
206
+
:::note
207
+
The `s3control` endpoint is required because recent versions of the AWS Terraform provider use the S3 Control API to read bucket tags.
208
+
We point it at `localhost.localstack.cloud` so the account-id-prefixed hostname (`<account-id>.localhost.localstack.cloud`) resolves to LocalStack.
209
+
:::
210
+
200
211
With the provider configured, we can now configure the variables for our S3 bucket.
201
212
Create a new file named `variables.tf` and add the following code:
202
213
@@ -230,15 +241,18 @@ output "name" {
230
241
231
242
output "domain" {
232
243
description = "Domain name of the bucket"
233
-
value = aws_s3_bucket_website_configuration.s3_bucket.website_domain
244
+
value = "s3-website.localhost.localstack.cloud:4566"
234
245
}
235
246
236
247
output "website_endpoint" {
237
-
value = aws_s3_bucket_website_configuration.s3_bucket.website_endpoint
248
+
description = "Website endpoint URL"
249
+
value = "http://${aws_s3_bucket.s3_bucket.id}.s3-website.localhost.localstack.cloud:4566"
238
250
}
239
251
```
240
252
241
-
The output variables are the ARN, name, domain name, and website endpoint of the bucket.
253
+
The output variables are the ARN, name, LocalStack S3 website domain, and the full website endpoint URL of the bucket.
254
+
We hardcode the `domain` and `website_endpoint` values to point at LocalStack so that the outputs surface a URL you can open directly.
255
+
The native `aws_s3_bucket_website_configuration` attributes return the AWS-formatted endpoint (`<bucket>.s3-website-<region>.amazonaws.com`), which would be misleading in a LocalStack-only setup.
242
256
With all the configuration files in place, we can now create the S3 bucket.
243
257
Create a new file named `main.tf` and create the S3 bucket using the following code:
244
258
@@ -305,7 +319,7 @@ Add the following code to the `main.tf` file:
The above code uploads all our html files to the bucket.
319
-
We are also setting the ACL of the files to `public-read`.
320
-
Optionally, if you have static assets like images, CSS, and JavaScript files, you can upload them to the bucket using the same `aws_s3_bucket_object` resource by adding the following code to the `main.tf` file:
321
-
322
-
```hcl showLineNumbers
323
-
resource "aws_s3_object" "object_assets" {
324
-
depends_on = [aws_s3_bucket.s3_bucket]
325
-
for_each = fileset(path.module, "assets/*")
326
-
bucket = var.bucket_name
327
-
key = each.value
328
-
source = "${each.value}"
329
-
etag = filemd5("${each.value}")
330
-
acl = "public-read"
331
-
}
332
-
```
333
-
332
+
The above code uploads every `.html` file under `www/` to the bucket and sets each object's ACL to `public-read`.
334
333
With all the configuration files in place, we can now initialize the Terraform configuration.
335
334
Run the following command to initialize the Terraform configuration:
In the above command, we specified `testbucket` as the bucket name.
369
+
In the above command, we specified `testwebsite` as the bucket name to keep it consistent with the `awslocal` flow above and the testing commands further down.
371
370
You can specify any bucket name since LocalStack is ephemeral, and stopping your LocalStack container will delete all the created resources.
372
-
The above command output includes the ARN, name, domain name, and website endpoint of the bucket.
373
-
You can see the `website_endpoint` configured to use AWS S3 Website Endpoint.
374
-
You can now access the website using the bucket name in the following format: `http://<BUCKET_NAME>.s3-website.localhost.localstack.cloud:4566`.
375
-
Since the endpoint is configured to use `localhost.localstack.cloud`, no real AWS resources have been created.
371
+
The above command output includes the ARN, name, LocalStack website domain, and the website endpoint URL of the bucket.
372
+
You can navigate directly to the printed `website_endpoint` to view your site, since the endpoint uses `localhost.localstack.cloud`, no real AWS resources have been created.
376
373
377
374
You can optionally use the `tflocal` CLI as a drop-in replacement for the official Terraform CLI. `tflocal` uses the Terraform Override mechanism to create a temporary `localstack_providers_override.tf` file, which is deleted after the infrastructure is created.
378
375
It mitigates the need to create the `provider.tf` file manually.
0 commit comments