Get aws_lambda_event_source_mapping from sqs.tf and use it in lambda.tf - aws-lambda

I have my terraform setup this way:
lambda.tf
sqs.tf
In lambda.tf
locals {
my_lambda = aws_lambda_function.vdf_lambda["my-lambda"]
}
...
environment {
variables = {
"MY_QUEUE_URL" = local.my_queue.id
"MY_TRIGGER_ID"= local.my_queue_trigger.uuid
}
}
in sqs.tf
locals {
my_queue = aws_sqs_queue.fifo_queue["my-queue"]
my_queue_trigger = aws_lambda_event_source_mapping.my_lambda_trigger
}
...
resource "aws_lambda_event_source_mapping" "my_lambda_trigger" {
batch_size = 1
event_source_arn = aws_sqs_queue.fifo_queue["my-queue"].arn
function_name = local.my_lambda.function_name
}
When I run terraform plan I got this error:
Error: Cycle: local.my_lambda (expand), aws_lambda_event_source_mapping.my_lambda_trigger,
local.my_queue_trigger (expand), aws_lambda_function.my_lambdas
My guess is the trigger was not created yet when I try to get its UUID in lambda.tf. So how do I fix this? How to get the trigger UUID in lambda.tf?

Related

Accessing Custom Options from .desc file in Protobuf

I have a proto with the following definitions.
import "google/protobuf/descriptor.proto";
extend google.protobuf.FieldOptions{
optional bool is_key = 50002;
}
message Foo{
int64 id = 1 [(is_key) = true];
}
I generated a .desc file for the above. I was able to access all the Fields and Message defined by the FieldDescriptorProto and DescriptorProto types but not sure how to access the options defined and the value provided to it in this case is_key.
Could anyone provide me with a java version that could access the options from the .desc file
Not sure If this is the right way but all I was trying to do was read custom options from a descriptor_set.desc file.
I parsed the desc file, the problem is that it returns fileDescriptorProto Types. So I parsed them and built a dependencies graph. This helped me to create FileDescriptor Types.
Through FileDescriptor Types I was able to get Extensions and add them to extensionRegistry.
Post Which I parsed the .desc file once again but this time with extensionRegistry.
val filePathToFileDescriptorMap = mutableMapOf <String, FileDescriptor>(DescriptorProtos.getDescriptor().file.name to
DescriptorProtos.getDescriptor())
val extensionRegistry = ExtensionRegistry.newInstance()
fun buildDependencies(fileDescriptorProto: DescriptorProtos.FileDescriptorProto,
filePathToFileDescriptorProtoMap: Map<String, DescriptorProtos.FileDescriptorProto>) : FileDescriptor {
val dependencies = fileDescriptorProto.dependencyList.map { dependency ->
if(filePathToFileDescriptorMap.containsKey(dependency)) {
filePathToFileDescriptorMap[dependency]
}
else if(!filePathToFileDescriptorProtoMap.containsKey(dependency)) {
throw Exception("dependency not found $dependency")
}
else {
buildDependencies(filePathToFileDescriptorProtoMap[dependency]!!, filePathToFileDescriptorProtoMap)
}
}
filePathToFileDescriptorMap[fileDescriptorProto.name] =
FileDescriptor.buildFrom(fileDescriptorProto, dependencies.toTypedArray())
filePathToFileDescriptorMap[fileDescriptorProto.name]!!.extensions.forEach {
if(it.type == Descriptors.FieldDescriptor.Type.MESSAGE){
extensionRegistry.add(it, DynamicMessage.newBuilder(it.messageType).build())
}else{
extensionRegistry.add(it)
}
}
return filePathToFileDescriptorMap[fileDescriptorProto.name]!!
}
fun registerExtensions(){
val fileDescriptorSet = DescriptorProtos.FileDescriptorSet.parseFrom(
FileInputStream("src/generated/resources/desc_set.desc"),
)
val filePathToFileDescriptorProtoMap = fileDescriptorSet.fileList.associateBy { it.name }
fileDescriptorSet.fileList.forEach { fileDescriptorProto ->
if(!filePathToFileDescriptorMap.containsKey(fileDescriptorProto.name)){
buildDependencies(fileDescriptorProto, filePathToFileDescriptorProtoMap)
}
}
val fileDescriptorSetWithOptions = DescriptorProtos.FileDescriptorSet.parseFrom(
FileInputStream("src/generated/resources/desc_set.desc"),
extensionRegistry
)
println(fileDescriptorSetWithOptions)
}

How to add arguments to a org.gradle.api.Action?

I'm writing a custom Gradle plugin that registers an extension and uses Actions for configuring it so you can use it like
myExtension {
info {
someConfig("a") {
// config for someConfig["a"]
}
someConfig("b") {
// config for someConfig["b"]
}
}
}
Based on how much someConfig you define, the plugin created new Gradle tasks dynamically. I've seen some scenarios in which it'd useful to configure all someConfig with the same values, so instead of writing
myExtension {
info {
someConfig("a") {
someValue = x
}
someConfig("b") {
someValue = x
}
}
}
I was thinking in providing something like
myExtension {
info {
someConfig("a")
someConfig("b")
allConfigs { configName ->
someValue = x
}
}
}
When trying to code it, I've used an objectFactory for creating instances of Action so I can configure the extension values
val allMyConfigs = mutableMapOf<String, SomeConfig>()
fun someConfig(configName: String, init: Action<in EnvironmentInfo>) {
val myConfig = allMyConfigs.computeIfAbsent(env) {
objectFactory.newInstance(SomeConfig::class.java, env)
}
init.execute(environmentInfo)
}
but since Action is a (SomeConfig) -> Unit (i.e. a function receiving a SomeConfig instance) I'm unable to pass the config name (an String) as param
Is there a way to create an Action, so it's compatible with both build.gradle and build.gradle.kts files, that receives an extra parameter and maintain the SomeConfig as delegate?

Set variables in a list in Declarative Jenkinsfile

I have set some environment variables as below :
environment {
IMG_TARGET = "registry/cloud-environemnt-azu:1.x.x"
CREDENTIALS = 'credentials-token'
BUILD_DIR = 'Builddir'
DOMAIN_DIR = 'Domaindir'
BUILD_SOLUTION = 'Dir.Builddir.sln'
}
Some variables are used throughout the Jenkins pipeline and I am looking to separate them from environment variables, such that only credentials and image target are in environment variables.
Is there a way I can use global list/variable/map to store non-environment variables, like :
List<String> variables = [
BUILD_DIR = 'Builddir'
DOMAIN_DIR = 'Domaindir'
BUILD_SOLUTION = 'Dir.Builddir.sln'
]
If so, how can I refer a variable from this list? The variables are being referenced across multiple stages in the pipeline
You can have an environment section for your pipeline and if needed, within each stage define a new environment section to override exisiting variables or define new ones. Example:
pipeline {
agent any
environment {
FOO = "bar"
NAME = "Joe"
}
stages {
stage("Env Variables") {
environment {
NAME = "Alan" // overrides pipeline level NAME env variable
BUILD_NUMBER = "2" // overrides the default BUILD_NUMBER
}
steps {
echo "FOO = ${env.FOO}" // prints "FOO = bar"
echo "NAME = ${env.NAME}" // prints "NAME = Alan"
echo "BUILD_NUMBER = ${env.BUILD_NUMBER}" // prints "BUILD_NUMBER = 2"
script {
env.SOMETHING = "1" // creates env.SOMETHING variable
}
}
}
stage("Override Variables") {
steps {
script {
env.FOO = "IT DOES NOT WORK!" // it can't override env.FOO declared at the pipeline (or stage) level
env.SOMETHING = "2" // it can override env variable created imperatively
}
echo "FOO = ${env.FOO}" // prints "FOO = bar"
echo "SOMETHING = ${env.SOMETHING}" // prints "SOMETHING = 2"
withEnv(["FOO=foobar"]) { // it can override any env variable
echo "FOO = ${env.FOO}" // prints "FOO = foobar"
}
withEnv(["BUILD_NUMBER=1"]) {
echo "BUILD_NUMBER = ${env.BUILD_NUMBER}" // prints "BUILD_NUMBER = 1"
}
}
}
}
}
Another option is to use parameterised jobs in declarative pipelines and if you're interested, you can read more here.
You can set environment variable specific to each stage by including environment block within the stage like below.
pipeline {
agent any
stages {
stage ("Test Stage"){
environment {
TEST_VARIABLE=test
}
steps {
echo "Hello"
}
}
}
}
If you want all non environment variable into single component then you can use map instead of List, as map will store the data in key value pair and you can retrieve any key at any point of time, with List you cannot store the value as a key value pair.
pipeline {
agent any
stages {
stage ("Test Stage"){
steps {
script {
def testMap = [BUILD_DIR:'Builddir',DOMAIN_DIR:'Domaindir',BUILD_SOLUTION:'Dir.Builddir.sln']
testMap.each {
entry -> echo "${entry.key}"
}
echo "${testMap['DOMAIN_DIR']}"
}
}
}
}
}
Thanks,

Jenkins pipeline RejectedAccessException error

I am trying to setup a jenkins pipeline script that sends out an email when there is a job that have been running for more than 24 hours.
// Long running jobs
pipeline {
agent any
environment {
EMAIL_ALERT_TO = "address"
EMAIL_ALERT_CC = "address"
}
stages {
stage('def methods') {
steps {
script {
Jenkins.instance.getAllItems(Job).each(){ job -> job.isBuildable()
if (job.isBuilding()){
def myBuild= job.getLastBuild()
def runningSince= groovy.time.TimeCategory.minus( new Date(), myBuild.getTime() )
echo "myBuild = ${myBuild}"
echo "runningSince = ${runningSince}"
env.myBuild = myBuild
env.runningSince = runningSince
}
}
}
}
}
}
post {
// Email out the results
always {
script {
if (runningSince.hours >= 1){
mail to: "${env.EMAIL_ALERT_CC}",
cc: "${env.EMAIL_ALERT_CC}",
subject: "Long Running Jobs",
body: "Build: ${myBuild} ---- Has Been Running for ${runningSince.hours} hours:${runningSince.minutes} minutes"
}
}
}
}
}
I am seeing RejectedAccessException which appears to be related to arrays/list.
This is what I believe you are looking for
https://issues.jenkins-ci.org/browse/JENKINS-54952?page=com.atlassian.jira.plugin.system.issuetabpanels%3Achangehistory-tabpanel

are needsDisplayForKey/actionForKey overrides working correctly?

I'm trying to convert some code that works in Objective-C to Swift. The problem I'm running into is that needsDisplayForKey/actionForKey aren't getting called the same way. As far as I can tell, the custom key values aren't getting passed in correctly. Here is what I get when I debug it:
default value:
(String!) event = {
core = {
_baseAddress = Builtin.RawPointer = 0x00feee51 "onOrderIn"
_countAndFlags = 1073741833
_owner = Some {
Some = (instance_type = Builtin.RawPointer = 0x01026348 #"onOrderIn")
}
}
}
custom value (empty string passed in):
(String!) event = {
core = {
_baseAddress = Builtin.RawPointer = 0x0b418f79
_countAndFlags = 1073741833
_owner = Some {
Some = (instance_type = Builtin.RawPointer = 0x0b418f70 -> 0x006e38f0 (void *)0x006e38c8: __NSCFString)
}
}
}
I'm not sure what the relevant code might be. I'll just ask - has anyone else was able to define a custom implicit animation in Swift? Is there anything I need to keep in mind when moving over from Objective C?
override class func needsDisplayForKey(key: String!) -> Bool{
if key == "angleFrom" || key == "angleTo" {
return true;
}
return super.needsDisplayForKey(key)
}
override func actionForKey(event: String!) -> CAAction!{
if event == "angleFrom" || event == "angleTo" {
return self.makeAnimationForKey(event)
}
return super.actionForKey(event)
}
I got this working in Swift by using #NSManaged attribute in front of the variable declaration (where you would use the the #dynamic attribute in Objective-C)

Resources