This tool saves the last Terraform state in S3. These are the resources needed: terraform-cloud-backup
You can build this lambda on your own and upload it to S3, or you can fork this repo and set up the GitHub Secrets to having the complete workflow.
- Fork this repository
- Setup the following GitHub Secrets
- FUNCTION_NAME
- AWS_ACCESS_KEY_ID
- AWS_SECRET_ACCESS_KEY
- AWS_REGION
- TF_TOKEN (Terraform Cloud token)
- BUCKET (Bucket to save the Terraform states.)
- BUCKET_BUILDS
GOOS=linux GOARCH=amd64 CGO_ENABLED=0
go mod tidy
go build -ldflags="-s -w" -a -installsuffix cgo -o main main.go
zip main.zip main
- func DownloadBody(url string) (string, error)
- func GetLastStateVersion(workspaceId string) (*tfe.StateVersion, error)
- func Handler(ctx context.Context, event events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error)
- func SaveLastState(payload Payload) (string, error)
- func StatusModified(payload Payload) bool
- func UploadToS3(key string, body string) error
- type Payload
func DownloadBody
func DownloadBody(url string) (string, error)
func GetLastStateVersion
func GetLastStateVersion(workspaceId string) (*tfe.StateVersion, error)
func Handler
func Handler(ctx context.Context, event events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error)
func SaveLastState
func SaveLastState(payload Payload) (string, error)
func StatusModified
func StatusModified(payload Payload) bool
func UploadToS3
func UploadToS3(key string, body string) error
type Payload
type Payload struct {
PayloadVersion int `json:"payload_version"`
NotificationConfigurationID string `json:"notification_configuration_id"`
RunURL string `json:"run_url"`
RunID string `json:"run_id"`
RunMessage string `json:"run_message"`
RunCreatedAt time.Time `json:"run_created_at"`
RunCreatedBy string `json:"run_created_by"`
WorkspaceID string `json:"workspace_id"`
WorkspaceName string `json:"workspace_name"`
OrganizationName string `json:"organization_name"`
Notifications []struct {
Message string `json:"message"`
Trigger string `json:"trigger"`
RunStatus string `json:"run_status"`
RunUpdatedAt time.Time `json:"run_updated_at"`
RunUpdatedBy string `json:"run_updated_by"`
} `json:"notifications"`
}
Generated by gomarkdoc