可以使用custom S3 endpoints配置Terraform,似乎localstack可以为S3,SES,Cloudformation和其他少数服务创建本地堆栈。
问题是在Terraform配置中写什么来使用localstack的S3端点?
答案 0 :(得分:11)
Terraform没有正式支持" AWS-workalike"系统,因为它们通常与AWS本身有微妙的怪癖和差异。但是,它是在尽力而为的基础上得到支持的,并且如果localstack能够为Terraform的目的提供足够逼真的S3印象,则可能会有效。
根据localstack文档,默认情况下,S3 API在from collections import defaultdict, deque
import heapq
def LargestSupersets(setlists):
'''Computes, for each item in the input, the largest superset in the same input.
setlists: A list of lists, each of which represents a set of items. Items must be hashable.
'''
# First, build a table that maps each element in any input setlist to a list of records
# of the form (-size of setlist, index of setlist), one for each setlist that contains
# the corresponding element
element_to_entries = defaultdict(list)
for idx, setlist in enumerate(setlists):
entry = (-len(setlist), idx) # cheesy way to make an entry that sorts properly -- largest first
for element in setlist:
element_to_entries[element].append(entry)
# Within each entry, sort so that larger items come first, with ties broken arbitrarily by
# the set's index
for entries in element_to_entries.values():
entries.sort()
# Now build up the output by going over each setlist and walking over the entries list for
# each element in the setlist. Since the entries list for each element is sorted largest to
# smallest, the first entry we find that is in every entry set we pulled will be the largest
# element of the input that contains each item in this setlist. We are guaranteed to eventually
# find such an element because, at the very least, the item we're iterating on itself is in
# each entries list.
output = []
for idx, setlist in enumerate(setlists):
num_elements = len(setlist)
buckets = [element_to_entries[element] for element in setlist]
# We implement the search for an item that appears in every list by maintaining a heap and
# a queue. We have the invariants that:
# 1. The queue contains the n smallest items across all the buckets, in order
# 2. The heap contains the smallest item from each bucket that has not already passed through
# the queue.
smallest_entries_heap = []
smallest_entries_deque = deque([], num_elements)
for bucket_idx, bucket in enumerate(buckets):
smallest_entries_heap.append((bucket[0], bucket_idx, 0))
heapq.heapify(smallest_entries_heap)
while (len(smallest_entries_deque) < num_elements or
smallest_entries_deque[0] != smallest_entries_deque[num_elements - 1]):
# First extract the next smallest entry in the queue ...
(smallest_entry, bucket_idx, element_within_bucket_idx) = heapq.heappop(smallest_entries_heap)
smallest_entries_deque.append(smallest_entry)
# ... then add the next-smallest item from the bucket that we just removed an element from
if element_within_bucket_idx + 1 < len(buckets[bucket_idx]):
new_element = buckets[bucket_idx][element_within_bucket_idx + 1]
heapq.heappush(smallest_entries_heap, (new_element, bucket_idx, element_within_bucket_idx + 1))
output.append((idx, smallest_entries_deque[0][1]))
return output
公开,因此以这种方式设置自定义端点可能有效:
http://localhost:4572
根据localstack的功能,您可能需要设置其他一些设置:
provider "aws" {
endpoints {
s3 = "http://localhost:4572"
}
}
为存储桶和对象使用基于路径的寻址方案。s3_force_path_style
,因为localstack似乎缺少AWS令牌服务的实现。skip_credentials_validation
如果不使用IAM样式的凭据,则阻止Terraform尝试从EC2元数据API获取凭据。答案 1 :(得分:8)
建立@ martin-atkins的答案,这是一个与Localstack一起使用的示例Terraform文件:
provider "aws" {
region = "us-east-1"
access_key = "anaccesskey"
secret_key = "asecretkey"
skip_credentials_validation = true
skip_metadata_api_check = true
s3_force_path_style = true
endpoints {
s3 = "http://localhost:4572"
}
}
resource "aws_s3_bucket" "b" {
bucket = "my-tf-test-bucket"
acl = "public-read"
}