- Inspect an existing configuration (
domain.tf) - Understand and list exported attributes of the
time_staticresource - Reference those attributes in another resource (
local_file) - Initialize and apply your configuration
- Verify the final state
1. Inspect domain.tf
Navigate to your project folder and open the domain.tf file:
time_static resource block:
time_static
2. What Is time_static?
The time_static resource generates a fixed timestamp on apply. You don’t need to pass any arguments—OpenTofu handles everything.
The
time_static resource produces a stable timestamp similar to random_pet. You can use its exported attributes elsewhere in your configuration.3. Exported Attributes of time_static
According to the time_static documentation, the following attributes are available:
| Attribute | Description |
|---|---|
| id | Timestamp in RFC3339 format |
| rfc3339 | Same as id (RFC3339 formatted) |
| day | Day of the month (1–31) |
| month | Month of the year (1–12) |
| year | Four-digit year |
| hour | Hour of the day (0–23) |
| minute | Minute of the hour (0–59) |
| second | Second of the minute (0–59) |
| unix | Unix epoch time (seconds since 1970) |
4. Reference a time_static Attribute
To reference the id (RFC3339) of your time_update resource:
5. Write the Timestamp to a Local File
Openmain.tf and add a local_file resource that writes the timestamp to /root/time.txt:
6. Initialize and Apply
Run the following commands:time_static.time_updatelocal_file.time
7. Inspect the Applied Resources
Useopentofu show to inspect the resource state:
id of local_file.time?Answer:
f095dbo698d1e64f847e39f01fc9f7d4592ff48
8. Retrieve the rfc3339 Timestamp
From the same output, you can also see:
rfc3339 value?Answer:
2024-04-09T09:52:41Z
9. Final Configuration Snapshot
After apply, your resources look like this:Links and References
- OpenTofu Documentation: https://docs.opentofu.org/
- time_static resource
- OpenTofu CLI Reference