我正在尝试根据可选参数创建条件。选项是是否从我的 userData 脚本运行一些额外的安装以进行 EC2 部署。
参数和条件如下所示:
Parameters:
EnvType:
Description: Option to install New Relic Infrastructure.
Default: apm
Type: String
AllowedValues:
- apm
- +infra
Run Code Online (Sandbox Code Playgroud)
然后我的 EC2 资源与条件启动脚本
Resources:
Ec2Instance:
Type: AWS::EC2::Instance
Properties:
InstanceType: t2.micro
ImageId: ami-9c9443e3 #Amazon Linux AMI in Tokyo
KeyName: tokyocloudformation
IamInstanceProfile: 'S3EC2'
SecurityGroupIds:
- !Ref myNewSecurityGroup
UserData:
Condition: apmOnly
Fn::Base64:
|
#!/bin/bash
installstuff
Condition: addInfrastructureAgent
Fn::Base64:
|
#!/bin/bash
installstuff
installsomeotherstuff
Run Code Online (Sandbox Code Playgroud)
我收到的错误消息是:模板验证错误:模板格式错误:未解析的依赖项 [EnvTyp]。无法引用模板的条件块中的资源
我明白错误的意思,我相信,但它似乎与 AWS 给出的例子不符。
https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/conditions-section-structure.html。此 AWS 示例清楚地在条件块中使用了 !Ref。
EnvType:
Description: Environment type.
Default: test
Type: …Run Code Online (Sandbox Code Playgroud) 我一直在尝试重写以下内容以使用Stream API.我似乎无法弄清楚如何到Map(或字段)为System.setProperty(k,v)设置键值对
我的数据只是在它们之间有空格的行,它们分别分为键和值:
foo bar
mykey myvalue
nextkey nextvalue
Run Code Online (Sandbox Code Playgroud)
我的工作源代码在这里:
try {
Scanner scanner = new Scanner(Paths.get("/path/to/file.txt"));
while(scanner.hasNextLine()){
String line = scanner.nextLine();
String[] array = line.split(" ");
if(array.length == 2){
System.setProperty("twitter4j.oauth." + array[0], array[1]);
}
}
} catch (IOException e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
在这里放一个破碎的样本,虽然我已经遍布地图(呻吟)试图用流写它,但这里只是为了证明我尝试了一次迭代:-p
Stream<String> lines = null;
try {
lines = Files.lines(Paths.get("/Users/bellis/dev/data/twitter.txt"));
} catch (IOException e) {
e.printStackTrace();
}
String[] words = lines.collect((Collectors.joining("\n"))).split(" ");
System.out.println("twitter4j.oauth." + words[0] + " " + words[1]);
Run Code Online (Sandbox Code Playgroud)
上面当然是不正确的我知道有更好的方法来使用函数和其他常见的流成语来编写它,但我似乎无法做到正确.您如何建议使用Stream和功能API编写此代码?